Can I save prediction value in same csv file as a another column using panda python












1












$begingroup$


I have csv data file and I design LSTM model to predict values. Then I want to save that prediction value in same csv file. Can I do that? I tried using one code then in my csv file only had prediction values and delete other columns. Can anyone give me a suggestion for that.



import pandas as pd
import numpy as np
data = pd.read_csv('data1.csv')
pred1=fit1.predict(x_test)
real_test = scaler_y.inverse_transform(np.array(y_test).reshape ((len(y_test), 1))).astype(int)
pred1 = pd.DataFrame(pred1, columns=['pred1']).to_csv('data1.csv')









share|improve this question









$endgroup$

















    1












    $begingroup$


    I have csv data file and I design LSTM model to predict values. Then I want to save that prediction value in same csv file. Can I do that? I tried using one code then in my csv file only had prediction values and delete other columns. Can anyone give me a suggestion for that.



    import pandas as pd
    import numpy as np
    data = pd.read_csv('data1.csv')
    pred1=fit1.predict(x_test)
    real_test = scaler_y.inverse_transform(np.array(y_test).reshape ((len(y_test), 1))).astype(int)
    pred1 = pd.DataFrame(pred1, columns=['pred1']).to_csv('data1.csv')









    share|improve this question









    $endgroup$















      1












      1








      1





      $begingroup$


      I have csv data file and I design LSTM model to predict values. Then I want to save that prediction value in same csv file. Can I do that? I tried using one code then in my csv file only had prediction values and delete other columns. Can anyone give me a suggestion for that.



      import pandas as pd
      import numpy as np
      data = pd.read_csv('data1.csv')
      pred1=fit1.predict(x_test)
      real_test = scaler_y.inverse_transform(np.array(y_test).reshape ((len(y_test), 1))).astype(int)
      pred1 = pd.DataFrame(pred1, columns=['pred1']).to_csv('data1.csv')









      share|improve this question









      $endgroup$




      I have csv data file and I design LSTM model to predict values. Then I want to save that prediction value in same csv file. Can I do that? I tried using one code then in my csv file only had prediction values and delete other columns. Can anyone give me a suggestion for that.



      import pandas as pd
      import numpy as np
      data = pd.read_csv('data1.csv')
      pred1=fit1.predict(x_test)
      real_test = scaler_y.inverse_transform(np.array(y_test).reshape ((len(y_test), 1))).astype(int)
      pred1 = pd.DataFrame(pred1, columns=['pred1']).to_csv('data1.csv')






      python pandas






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 5 hours ago









      awaawa

      325




      325






















          2 Answers
          2






          active

          oldest

          votes


















          1












          $begingroup$

          if you want this column in the same dataframe just do



          data['pred'] = pred1
          data.to_csv('data1.csv')


          The first line automatically adds a column called 'pred' to the dataframe with values coming from pred1.



          Hope it helps. Good luck!






          share|improve this answer









          $endgroup$













          • $begingroup$
            I tried your code and it gave me an error Length of values does not match length of index. May be because of less value prediction. If I want to save my prediction values comparatively to the actual value.Then how can I code it according to the actual value save prediction value in samme csv file?
            $endgroup$
            – awa
            47 mins ago










          • $begingroup$
            So the fact is that probably your "test" is a fraction of your data not all of it. You first need to take those rows that you chose for testing and keep it in a dataframe. Then add this column to that dataframe.
            $endgroup$
            – Kasra Manshaei
            10 mins ago



















          1












          $begingroup$

          There is no direct method for it but you can do it by the following simple manipulation. Instead of directly appending to the csv file you can open it in python and then append it. Here is the code for the same:



          data = pd.read_csv("data1.csv")
          data['pred1'] = pred1
          df.to_csv('data1.csv')





          share|improve this answer









          $endgroup$













          • $begingroup$
            I tried your code and it gave me an error Length of values does not match length of index. May be because of less value prediction. If I want to save my prediction values comparatively to the actual value.Then how can I code it according to the actual value save prediction value in samme csv file?
            $endgroup$
            – awa
            46 mins ago











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "557"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f45074%2fcan-i-save-prediction-value-in-same-csv-file-as-a-another-column-using-panda-pyt%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1












          $begingroup$

          if you want this column in the same dataframe just do



          data['pred'] = pred1
          data.to_csv('data1.csv')


          The first line automatically adds a column called 'pred' to the dataframe with values coming from pred1.



          Hope it helps. Good luck!






          share|improve this answer









          $endgroup$













          • $begingroup$
            I tried your code and it gave me an error Length of values does not match length of index. May be because of less value prediction. If I want to save my prediction values comparatively to the actual value.Then how can I code it according to the actual value save prediction value in samme csv file?
            $endgroup$
            – awa
            47 mins ago










          • $begingroup$
            So the fact is that probably your "test" is a fraction of your data not all of it. You first need to take those rows that you chose for testing and keep it in a dataframe. Then add this column to that dataframe.
            $endgroup$
            – Kasra Manshaei
            10 mins ago
















          1












          $begingroup$

          if you want this column in the same dataframe just do



          data['pred'] = pred1
          data.to_csv('data1.csv')


          The first line automatically adds a column called 'pred' to the dataframe with values coming from pred1.



          Hope it helps. Good luck!






          share|improve this answer









          $endgroup$













          • $begingroup$
            I tried your code and it gave me an error Length of values does not match length of index. May be because of less value prediction. If I want to save my prediction values comparatively to the actual value.Then how can I code it according to the actual value save prediction value in samme csv file?
            $endgroup$
            – awa
            47 mins ago










          • $begingroup$
            So the fact is that probably your "test" is a fraction of your data not all of it. You first need to take those rows that you chose for testing and keep it in a dataframe. Then add this column to that dataframe.
            $endgroup$
            – Kasra Manshaei
            10 mins ago














          1












          1








          1





          $begingroup$

          if you want this column in the same dataframe just do



          data['pred'] = pred1
          data.to_csv('data1.csv')


          The first line automatically adds a column called 'pred' to the dataframe with values coming from pred1.



          Hope it helps. Good luck!






          share|improve this answer









          $endgroup$



          if you want this column in the same dataframe just do



          data['pred'] = pred1
          data.to_csv('data1.csv')


          The first line automatically adds a column called 'pred' to the dataframe with values coming from pred1.



          Hope it helps. Good luck!







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 5 hours ago









          Kasra ManshaeiKasra Manshaei

          3,7041035




          3,7041035












          • $begingroup$
            I tried your code and it gave me an error Length of values does not match length of index. May be because of less value prediction. If I want to save my prediction values comparatively to the actual value.Then how can I code it according to the actual value save prediction value in samme csv file?
            $endgroup$
            – awa
            47 mins ago










          • $begingroup$
            So the fact is that probably your "test" is a fraction of your data not all of it. You first need to take those rows that you chose for testing and keep it in a dataframe. Then add this column to that dataframe.
            $endgroup$
            – Kasra Manshaei
            10 mins ago


















          • $begingroup$
            I tried your code and it gave me an error Length of values does not match length of index. May be because of less value prediction. If I want to save my prediction values comparatively to the actual value.Then how can I code it according to the actual value save prediction value in samme csv file?
            $endgroup$
            – awa
            47 mins ago










          • $begingroup$
            So the fact is that probably your "test" is a fraction of your data not all of it. You first need to take those rows that you chose for testing and keep it in a dataframe. Then add this column to that dataframe.
            $endgroup$
            – Kasra Manshaei
            10 mins ago
















          $begingroup$
          I tried your code and it gave me an error Length of values does not match length of index. May be because of less value prediction. If I want to save my prediction values comparatively to the actual value.Then how can I code it according to the actual value save prediction value in samme csv file?
          $endgroup$
          – awa
          47 mins ago




          $begingroup$
          I tried your code and it gave me an error Length of values does not match length of index. May be because of less value prediction. If I want to save my prediction values comparatively to the actual value.Then how can I code it according to the actual value save prediction value in samme csv file?
          $endgroup$
          – awa
          47 mins ago












          $begingroup$
          So the fact is that probably your "test" is a fraction of your data not all of it. You first need to take those rows that you chose for testing and keep it in a dataframe. Then add this column to that dataframe.
          $endgroup$
          – Kasra Manshaei
          10 mins ago




          $begingroup$
          So the fact is that probably your "test" is a fraction of your data not all of it. You first need to take those rows that you chose for testing and keep it in a dataframe. Then add this column to that dataframe.
          $endgroup$
          – Kasra Manshaei
          10 mins ago











          1












          $begingroup$

          There is no direct method for it but you can do it by the following simple manipulation. Instead of directly appending to the csv file you can open it in python and then append it. Here is the code for the same:



          data = pd.read_csv("data1.csv")
          data['pred1'] = pred1
          df.to_csv('data1.csv')





          share|improve this answer









          $endgroup$













          • $begingroup$
            I tried your code and it gave me an error Length of values does not match length of index. May be because of less value prediction. If I want to save my prediction values comparatively to the actual value.Then how can I code it according to the actual value save prediction value in samme csv file?
            $endgroup$
            – awa
            46 mins ago
















          1












          $begingroup$

          There is no direct method for it but you can do it by the following simple manipulation. Instead of directly appending to the csv file you can open it in python and then append it. Here is the code for the same:



          data = pd.read_csv("data1.csv")
          data['pred1'] = pred1
          df.to_csv('data1.csv')





          share|improve this answer









          $endgroup$













          • $begingroup$
            I tried your code and it gave me an error Length of values does not match length of index. May be because of less value prediction. If I want to save my prediction values comparatively to the actual value.Then how can I code it according to the actual value save prediction value in samme csv file?
            $endgroup$
            – awa
            46 mins ago














          1












          1








          1





          $begingroup$

          There is no direct method for it but you can do it by the following simple manipulation. Instead of directly appending to the csv file you can open it in python and then append it. Here is the code for the same:



          data = pd.read_csv("data1.csv")
          data['pred1'] = pred1
          df.to_csv('data1.csv')





          share|improve this answer









          $endgroup$



          There is no direct method for it but you can do it by the following simple manipulation. Instead of directly appending to the csv file you can open it in python and then append it. Here is the code for the same:



          data = pd.read_csv("data1.csv")
          data['pred1'] = pred1
          df.to_csv('data1.csv')






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 5 hours ago









          bkshibkshi

          3159




          3159












          • $begingroup$
            I tried your code and it gave me an error Length of values does not match length of index. May be because of less value prediction. If I want to save my prediction values comparatively to the actual value.Then how can I code it according to the actual value save prediction value in samme csv file?
            $endgroup$
            – awa
            46 mins ago


















          • $begingroup$
            I tried your code and it gave me an error Length of values does not match length of index. May be because of less value prediction. If I want to save my prediction values comparatively to the actual value.Then how can I code it according to the actual value save prediction value in samme csv file?
            $endgroup$
            – awa
            46 mins ago
















          $begingroup$
          I tried your code and it gave me an error Length of values does not match length of index. May be because of less value prediction. If I want to save my prediction values comparatively to the actual value.Then how can I code it according to the actual value save prediction value in samme csv file?
          $endgroup$
          – awa
          46 mins ago




          $begingroup$
          I tried your code and it gave me an error Length of values does not match length of index. May be because of less value prediction. If I want to save my prediction values comparatively to the actual value.Then how can I code it according to the actual value save prediction value in samme csv file?
          $endgroup$
          – awa
          46 mins ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Data Science Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f45074%2fcan-i-save-prediction-value-in-same-csv-file-as-a-another-column-using-panda-pyt%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Cáceres

          Lexus RX

          Plaza Victoria