If values from two columns in two different data frames match then copy values from another column in df2 to...











up vote
0
down vote

favorite












I have two data frames, and I want to copy values from the column "Result2" in df2 to column "Result1" in df1 and to add the column "Description" from df2 to df1 for values that are in the same row as values from the column "Result2" (that are already copied), if the values from columns "ID" in both data frames match.



Here are the data frames:



df1



ID              Result1
Position1 NaN
MRS1 OK
MRS2 Maybe
MRS3 NOK
Position2 NaN
MRS4 OK
MRS5 OK
Position3 NaN
MRS6 OK
Position4 NaN
MRS7 NOK
MRS8 OK
MRS9 NOK
MRS10 Excellent


df2



ID              Result2              Description
text NaN NaN
NaN NaN text
text NaN NaN
MRS1 OK New
MRS2 OK Old
NaN NaN NaN
NaN NaN text
MRS3 OK New
MRS4 OK New
MRS5 OK NaN
MRS6 NOK Very old
NaN text NaN
text text NaN
NaN NaN NaN
MRS7 Excellent Very old
MRS8 Excellent Old
text text text
MRS9 Excellent New
NaN NaN NaN
MRS10 NOK New


I want to get:



ID              Result1             Result2
Position1 NaN NaN
MRS1 OK New
MRS2 OK Old
MRS3 OK New
Position2 NaN NaN
MRS4 OK New
MRS5 OK NaN
Position3 NaN NaN
MRS6 NOK Very old
Position4 NaN NaN
MRS7 Excellent Very old
MRS8 Excellent Old
MRS9 Excellent New
MRS10 NOK New


and I put NaN on the places where should be an empty cell. How to do this in pandas?
I tried with the merge, but I have the problem because it is required the same length of comparing columns. I just want to compare values from the "ID" columns from both data frames, and those columns do not have the same length.










share|improve this question




























    up vote
    0
    down vote

    favorite












    I have two data frames, and I want to copy values from the column "Result2" in df2 to column "Result1" in df1 and to add the column "Description" from df2 to df1 for values that are in the same row as values from the column "Result2" (that are already copied), if the values from columns "ID" in both data frames match.



    Here are the data frames:



    df1



    ID              Result1
    Position1 NaN
    MRS1 OK
    MRS2 Maybe
    MRS3 NOK
    Position2 NaN
    MRS4 OK
    MRS5 OK
    Position3 NaN
    MRS6 OK
    Position4 NaN
    MRS7 NOK
    MRS8 OK
    MRS9 NOK
    MRS10 Excellent


    df2



    ID              Result2              Description
    text NaN NaN
    NaN NaN text
    text NaN NaN
    MRS1 OK New
    MRS2 OK Old
    NaN NaN NaN
    NaN NaN text
    MRS3 OK New
    MRS4 OK New
    MRS5 OK NaN
    MRS6 NOK Very old
    NaN text NaN
    text text NaN
    NaN NaN NaN
    MRS7 Excellent Very old
    MRS8 Excellent Old
    text text text
    MRS9 Excellent New
    NaN NaN NaN
    MRS10 NOK New


    I want to get:



    ID              Result1             Result2
    Position1 NaN NaN
    MRS1 OK New
    MRS2 OK Old
    MRS3 OK New
    Position2 NaN NaN
    MRS4 OK New
    MRS5 OK NaN
    Position3 NaN NaN
    MRS6 NOK Very old
    Position4 NaN NaN
    MRS7 Excellent Very old
    MRS8 Excellent Old
    MRS9 Excellent New
    MRS10 NOK New


    and I put NaN on the places where should be an empty cell. How to do this in pandas?
    I tried with the merge, but I have the problem because it is required the same length of comparing columns. I just want to compare values from the "ID" columns from both data frames, and those columns do not have the same length.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have two data frames, and I want to copy values from the column "Result2" in df2 to column "Result1" in df1 and to add the column "Description" from df2 to df1 for values that are in the same row as values from the column "Result2" (that are already copied), if the values from columns "ID" in both data frames match.



      Here are the data frames:



      df1



      ID              Result1
      Position1 NaN
      MRS1 OK
      MRS2 Maybe
      MRS3 NOK
      Position2 NaN
      MRS4 OK
      MRS5 OK
      Position3 NaN
      MRS6 OK
      Position4 NaN
      MRS7 NOK
      MRS8 OK
      MRS9 NOK
      MRS10 Excellent


      df2



      ID              Result2              Description
      text NaN NaN
      NaN NaN text
      text NaN NaN
      MRS1 OK New
      MRS2 OK Old
      NaN NaN NaN
      NaN NaN text
      MRS3 OK New
      MRS4 OK New
      MRS5 OK NaN
      MRS6 NOK Very old
      NaN text NaN
      text text NaN
      NaN NaN NaN
      MRS7 Excellent Very old
      MRS8 Excellent Old
      text text text
      MRS9 Excellent New
      NaN NaN NaN
      MRS10 NOK New


      I want to get:



      ID              Result1             Result2
      Position1 NaN NaN
      MRS1 OK New
      MRS2 OK Old
      MRS3 OK New
      Position2 NaN NaN
      MRS4 OK New
      MRS5 OK NaN
      Position3 NaN NaN
      MRS6 NOK Very old
      Position4 NaN NaN
      MRS7 Excellent Very old
      MRS8 Excellent Old
      MRS9 Excellent New
      MRS10 NOK New


      and I put NaN on the places where should be an empty cell. How to do this in pandas?
      I tried with the merge, but I have the problem because it is required the same length of comparing columns. I just want to compare values from the "ID" columns from both data frames, and those columns do not have the same length.










      share|improve this question















      I have two data frames, and I want to copy values from the column "Result2" in df2 to column "Result1" in df1 and to add the column "Description" from df2 to df1 for values that are in the same row as values from the column "Result2" (that are already copied), if the values from columns "ID" in both data frames match.



      Here are the data frames:



      df1



      ID              Result1
      Position1 NaN
      MRS1 OK
      MRS2 Maybe
      MRS3 NOK
      Position2 NaN
      MRS4 OK
      MRS5 OK
      Position3 NaN
      MRS6 OK
      Position4 NaN
      MRS7 NOK
      MRS8 OK
      MRS9 NOK
      MRS10 Excellent


      df2



      ID              Result2              Description
      text NaN NaN
      NaN NaN text
      text NaN NaN
      MRS1 OK New
      MRS2 OK Old
      NaN NaN NaN
      NaN NaN text
      MRS3 OK New
      MRS4 OK New
      MRS5 OK NaN
      MRS6 NOK Very old
      NaN text NaN
      text text NaN
      NaN NaN NaN
      MRS7 Excellent Very old
      MRS8 Excellent Old
      text text text
      MRS9 Excellent New
      NaN NaN NaN
      MRS10 NOK New


      I want to get:



      ID              Result1             Result2
      Position1 NaN NaN
      MRS1 OK New
      MRS2 OK Old
      MRS3 OK New
      Position2 NaN NaN
      MRS4 OK New
      MRS5 OK NaN
      Position3 NaN NaN
      MRS6 NOK Very old
      Position4 NaN NaN
      MRS7 Excellent Very old
      MRS8 Excellent Old
      MRS9 Excellent New
      MRS10 NOK New


      and I put NaN on the places where should be an empty cell. How to do this in pandas?
      I tried with the merge, but I have the problem because it is required the same length of comparing columns. I just want to compare values from the "ID" columns from both data frames, and those columns do not have the same length.







      python pandas dataframe matching






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 7:05

























      asked Nov 20 at 7:00









      slobokv83

      427




      427
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Use merge with only column ID with left join and rename columns:



          df = (df1[['ID']].merge(df2, on='ID', how='left')
          .rename(columns={'Result2':'Result1','Description':'Result2'}))
          print (df)
          ID Result1 Result2
          0 Position1 NaN NaN
          1 MRS1 OK New
          2 MRS2 OK Old
          3 MRS3 OK New
          4 Position2 NaN NaN
          5 MRS4 OK New
          6 MRS5 OK NaN
          7 Position3 NaN NaN
          8 MRS6 NOK Very old
          9 Position4 NaN NaN
          10 MRS7 Excellent Very old
          11 MRS8 Excellent Old
          12 MRS9 Excellent New
          13 MRS10 NOK New





          share|improve this answer

















          • 1




            Thank you very much. It works very well. I made one mistake, I wanted to keep the name of the column "Description", and I typed "Result2" instead.
            – slobokv83
            Nov 20 at 7:32










          • Could I ask you if I have one more column in df1 which I need to keep in final, what I should do? That column has the same length as "ID" in df1.
            – slobokv83
            Nov 20 at 9:35












          • @slobokv83 - only change df1[['ID']] to df1
            – jezrael
            Nov 20 at 9:36






          • 1




            Thank you very much.
            – slobokv83
            Nov 20 at 9:37










          • sorry because I ask you like this, but is it a way to import Excel data to ppt slides? I found the python-pptx library and I use it, but I could not find a way to import data from excel. Also, I could not find a way to keep formatting when I read Excel file, but I could set it afterward.
            – slobokv83
            Nov 20 at 10:01











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          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',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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%2fstackoverflow.com%2fquestions%2f53387782%2fif-values-from-two-columns-in-two-different-data-frames-match-then-copy-values-f%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          Use merge with only column ID with left join and rename columns:



          df = (df1[['ID']].merge(df2, on='ID', how='left')
          .rename(columns={'Result2':'Result1','Description':'Result2'}))
          print (df)
          ID Result1 Result2
          0 Position1 NaN NaN
          1 MRS1 OK New
          2 MRS2 OK Old
          3 MRS3 OK New
          4 Position2 NaN NaN
          5 MRS4 OK New
          6 MRS5 OK NaN
          7 Position3 NaN NaN
          8 MRS6 NOK Very old
          9 Position4 NaN NaN
          10 MRS7 Excellent Very old
          11 MRS8 Excellent Old
          12 MRS9 Excellent New
          13 MRS10 NOK New





          share|improve this answer

















          • 1




            Thank you very much. It works very well. I made one mistake, I wanted to keep the name of the column "Description", and I typed "Result2" instead.
            – slobokv83
            Nov 20 at 7:32










          • Could I ask you if I have one more column in df1 which I need to keep in final, what I should do? That column has the same length as "ID" in df1.
            – slobokv83
            Nov 20 at 9:35












          • @slobokv83 - only change df1[['ID']] to df1
            – jezrael
            Nov 20 at 9:36






          • 1




            Thank you very much.
            – slobokv83
            Nov 20 at 9:37










          • sorry because I ask you like this, but is it a way to import Excel data to ppt slides? I found the python-pptx library and I use it, but I could not find a way to import data from excel. Also, I could not find a way to keep formatting when I read Excel file, but I could set it afterward.
            – slobokv83
            Nov 20 at 10:01















          up vote
          1
          down vote



          accepted










          Use merge with only column ID with left join and rename columns:



          df = (df1[['ID']].merge(df2, on='ID', how='left')
          .rename(columns={'Result2':'Result1','Description':'Result2'}))
          print (df)
          ID Result1 Result2
          0 Position1 NaN NaN
          1 MRS1 OK New
          2 MRS2 OK Old
          3 MRS3 OK New
          4 Position2 NaN NaN
          5 MRS4 OK New
          6 MRS5 OK NaN
          7 Position3 NaN NaN
          8 MRS6 NOK Very old
          9 Position4 NaN NaN
          10 MRS7 Excellent Very old
          11 MRS8 Excellent Old
          12 MRS9 Excellent New
          13 MRS10 NOK New





          share|improve this answer

















          • 1




            Thank you very much. It works very well. I made one mistake, I wanted to keep the name of the column "Description", and I typed "Result2" instead.
            – slobokv83
            Nov 20 at 7:32










          • Could I ask you if I have one more column in df1 which I need to keep in final, what I should do? That column has the same length as "ID" in df1.
            – slobokv83
            Nov 20 at 9:35












          • @slobokv83 - only change df1[['ID']] to df1
            – jezrael
            Nov 20 at 9:36






          • 1




            Thank you very much.
            – slobokv83
            Nov 20 at 9:37










          • sorry because I ask you like this, but is it a way to import Excel data to ppt slides? I found the python-pptx library and I use it, but I could not find a way to import data from excel. Also, I could not find a way to keep formatting when I read Excel file, but I could set it afterward.
            – slobokv83
            Nov 20 at 10:01













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          Use merge with only column ID with left join and rename columns:



          df = (df1[['ID']].merge(df2, on='ID', how='left')
          .rename(columns={'Result2':'Result1','Description':'Result2'}))
          print (df)
          ID Result1 Result2
          0 Position1 NaN NaN
          1 MRS1 OK New
          2 MRS2 OK Old
          3 MRS3 OK New
          4 Position2 NaN NaN
          5 MRS4 OK New
          6 MRS5 OK NaN
          7 Position3 NaN NaN
          8 MRS6 NOK Very old
          9 Position4 NaN NaN
          10 MRS7 Excellent Very old
          11 MRS8 Excellent Old
          12 MRS9 Excellent New
          13 MRS10 NOK New





          share|improve this answer












          Use merge with only column ID with left join and rename columns:



          df = (df1[['ID']].merge(df2, on='ID', how='left')
          .rename(columns={'Result2':'Result1','Description':'Result2'}))
          print (df)
          ID Result1 Result2
          0 Position1 NaN NaN
          1 MRS1 OK New
          2 MRS2 OK Old
          3 MRS3 OK New
          4 Position2 NaN NaN
          5 MRS4 OK New
          6 MRS5 OK NaN
          7 Position3 NaN NaN
          8 MRS6 NOK Very old
          9 Position4 NaN NaN
          10 MRS7 Excellent Very old
          11 MRS8 Excellent Old
          12 MRS9 Excellent New
          13 MRS10 NOK New






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 7:04









          jezrael

          315k21252330




          315k21252330








          • 1




            Thank you very much. It works very well. I made one mistake, I wanted to keep the name of the column "Description", and I typed "Result2" instead.
            – slobokv83
            Nov 20 at 7:32










          • Could I ask you if I have one more column in df1 which I need to keep in final, what I should do? That column has the same length as "ID" in df1.
            – slobokv83
            Nov 20 at 9:35












          • @slobokv83 - only change df1[['ID']] to df1
            – jezrael
            Nov 20 at 9:36






          • 1




            Thank you very much.
            – slobokv83
            Nov 20 at 9:37










          • sorry because I ask you like this, but is it a way to import Excel data to ppt slides? I found the python-pptx library and I use it, but I could not find a way to import data from excel. Also, I could not find a way to keep formatting when I read Excel file, but I could set it afterward.
            – slobokv83
            Nov 20 at 10:01














          • 1




            Thank you very much. It works very well. I made one mistake, I wanted to keep the name of the column "Description", and I typed "Result2" instead.
            – slobokv83
            Nov 20 at 7:32










          • Could I ask you if I have one more column in df1 which I need to keep in final, what I should do? That column has the same length as "ID" in df1.
            – slobokv83
            Nov 20 at 9:35












          • @slobokv83 - only change df1[['ID']] to df1
            – jezrael
            Nov 20 at 9:36






          • 1




            Thank you very much.
            – slobokv83
            Nov 20 at 9:37










          • sorry because I ask you like this, but is it a way to import Excel data to ppt slides? I found the python-pptx library and I use it, but I could not find a way to import data from excel. Also, I could not find a way to keep formatting when I read Excel file, but I could set it afterward.
            – slobokv83
            Nov 20 at 10:01








          1




          1




          Thank you very much. It works very well. I made one mistake, I wanted to keep the name of the column "Description", and I typed "Result2" instead.
          – slobokv83
          Nov 20 at 7:32




          Thank you very much. It works very well. I made one mistake, I wanted to keep the name of the column "Description", and I typed "Result2" instead.
          – slobokv83
          Nov 20 at 7:32












          Could I ask you if I have one more column in df1 which I need to keep in final, what I should do? That column has the same length as "ID" in df1.
          – slobokv83
          Nov 20 at 9:35






          Could I ask you if I have one more column in df1 which I need to keep in final, what I should do? That column has the same length as "ID" in df1.
          – slobokv83
          Nov 20 at 9:35














          @slobokv83 - only change df1[['ID']] to df1
          – jezrael
          Nov 20 at 9:36




          @slobokv83 - only change df1[['ID']] to df1
          – jezrael
          Nov 20 at 9:36




          1




          1




          Thank you very much.
          – slobokv83
          Nov 20 at 9:37




          Thank you very much.
          – slobokv83
          Nov 20 at 9:37












          sorry because I ask you like this, but is it a way to import Excel data to ppt slides? I found the python-pptx library and I use it, but I could not find a way to import data from excel. Also, I could not find a way to keep formatting when I read Excel file, but I could set it afterward.
          – slobokv83
          Nov 20 at 10:01




          sorry because I ask you like this, but is it a way to import Excel data to ppt slides? I found the python-pptx library and I use it, but I could not find a way to import data from excel. Also, I could not find a way to keep formatting when I read Excel file, but I could set it afterward.
          – slobokv83
          Nov 20 at 10:01


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • 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.


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





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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.


          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%2fstackoverflow.com%2fquestions%2f53387782%2fif-values-from-two-columns-in-two-different-data-frames-match-then-copy-values-f%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

          To store a contact into the json file from server.js file using a class in NodeJS

          Redirect URL with Chrome Remote Debugging Android Devices

          Dieringhausen