How to Join CSV in Python Pandas Comparing 2 CSV










0















I have 2 csv files lets say A.csv and B.csv. A.csv has columns a,b,c,d and B.csv has columns x,y,z,t. I want to search that if an entry in column a exist in column x then print z and d if that rows.



Like,



for each i in A
if A.[a][i] exist in B.x
print A.[d][i] + B.[z][i]


-- I have already the code below. I just need to learn how to settle the code for this



 import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


A = pd.read_csv('path1')
B = pd.read_csv('path2')









share|improve this question



















  • 1





    Could you please provide sample dataframes for A and B and an expected output?

    – Scotty1-
    Nov 15 '18 at 13:55















0















I have 2 csv files lets say A.csv and B.csv. A.csv has columns a,b,c,d and B.csv has columns x,y,z,t. I want to search that if an entry in column a exist in column x then print z and d if that rows.



Like,



for each i in A
if A.[a][i] exist in B.x
print A.[d][i] + B.[z][i]


-- I have already the code below. I just need to learn how to settle the code for this



 import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


A = pd.read_csv('path1')
B = pd.read_csv('path2')









share|improve this question



















  • 1





    Could you please provide sample dataframes for A and B and an expected output?

    – Scotty1-
    Nov 15 '18 at 13:55













0












0








0








I have 2 csv files lets say A.csv and B.csv. A.csv has columns a,b,c,d and B.csv has columns x,y,z,t. I want to search that if an entry in column a exist in column x then print z and d if that rows.



Like,



for each i in A
if A.[a][i] exist in B.x
print A.[d][i] + B.[z][i]


-- I have already the code below. I just need to learn how to settle the code for this



 import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


A = pd.read_csv('path1')
B = pd.read_csv('path2')









share|improve this question
















I have 2 csv files lets say A.csv and B.csv. A.csv has columns a,b,c,d and B.csv has columns x,y,z,t. I want to search that if an entry in column a exist in column x then print z and d if that rows.



Like,



for each i in A
if A.[a][i] exist in B.x
print A.[d][i] + B.[z][i]


-- I have already the code below. I just need to learn how to settle the code for this



 import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


A = pd.read_csv('path1')
B = pd.read_csv('path2')






python pandas csv numpy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 14:04









chthonicdaemon

12.5k3147




12.5k3147










asked Nov 15 '18 at 13:54









abidinberkayabidinberkay

62411031




62411031







  • 1





    Could you please provide sample dataframes for A and B and an expected output?

    – Scotty1-
    Nov 15 '18 at 13:55












  • 1





    Could you please provide sample dataframes for A and B and an expected output?

    – Scotty1-
    Nov 15 '18 at 13:55







1




1





Could you please provide sample dataframes for A and B and an expected output?

– Scotty1-
Nov 15 '18 at 13:55





Could you please provide sample dataframes for A and B and an expected output?

– Scotty1-
Nov 15 '18 at 13:55












2 Answers
2






active

oldest

votes


















2














Imagine your csv data file looks like below:



print(df1)

A B C D
0 1 4 7 4
1 2 5 8 5
2 3 6 9 8

print(df2

X Y Z T
0 1 11 6 8
1 5 12 8 0
2 2 13 0 4


A simple merge would solve your problem, considering Left table is df1 and Right is df2



df = df1.merge(df2,left_on='A',right_on='X')[['Z','D']]

print(df)

Z D
0 6 4
1 0 5


This will return the matched row elements from mentioned columns. (here Z,D)






share|improve this answer






























    1














    I think the simplest solution is to use a left join:



    >>> print(A)
    a b c d
    0 1 2 3 4
    1 2 3 4 5
    2 4 4 5 6

    >>> print(B)

    x y z t
    0 1 20 30 40
    1 3 4 5 6

    >>> result = A.merge(left_on='a', right=B, right_on='x', how='left')[['z', 'd']].dropna()

    >>> print(result)
    z d
    0 30.0 4





    share|improve this answer






















      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',
      autoActivateHeartbeat: false,
      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%2f53321027%2fhow-to-join-csv-in-python-pandas-comparing-2-csv%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









      2














      Imagine your csv data file looks like below:



      print(df1)

      A B C D
      0 1 4 7 4
      1 2 5 8 5
      2 3 6 9 8

      print(df2

      X Y Z T
      0 1 11 6 8
      1 5 12 8 0
      2 2 13 0 4


      A simple merge would solve your problem, considering Left table is df1 and Right is df2



      df = df1.merge(df2,left_on='A',right_on='X')[['Z','D']]

      print(df)

      Z D
      0 6 4
      1 0 5


      This will return the matched row elements from mentioned columns. (here Z,D)






      share|improve this answer



























        2














        Imagine your csv data file looks like below:



        print(df1)

        A B C D
        0 1 4 7 4
        1 2 5 8 5
        2 3 6 9 8

        print(df2

        X Y Z T
        0 1 11 6 8
        1 5 12 8 0
        2 2 13 0 4


        A simple merge would solve your problem, considering Left table is df1 and Right is df2



        df = df1.merge(df2,left_on='A',right_on='X')[['Z','D']]

        print(df)

        Z D
        0 6 4
        1 0 5


        This will return the matched row elements from mentioned columns. (here Z,D)






        share|improve this answer

























          2












          2








          2







          Imagine your csv data file looks like below:



          print(df1)

          A B C D
          0 1 4 7 4
          1 2 5 8 5
          2 3 6 9 8

          print(df2

          X Y Z T
          0 1 11 6 8
          1 5 12 8 0
          2 2 13 0 4


          A simple merge would solve your problem, considering Left table is df1 and Right is df2



          df = df1.merge(df2,left_on='A',right_on='X')[['Z','D']]

          print(df)

          Z D
          0 6 4
          1 0 5


          This will return the matched row elements from mentioned columns. (here Z,D)






          share|improve this answer













          Imagine your csv data file looks like below:



          print(df1)

          A B C D
          0 1 4 7 4
          1 2 5 8 5
          2 3 6 9 8

          print(df2

          X Y Z T
          0 1 11 6 8
          1 5 12 8 0
          2 2 13 0 4


          A simple merge would solve your problem, considering Left table is df1 and Right is df2



          df = df1.merge(df2,left_on='A',right_on='X')[['Z','D']]

          print(df)

          Z D
          0 6 4
          1 0 5


          This will return the matched row elements from mentioned columns. (here Z,D)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 17:22









          ManojNiraleManojNirale

          13410




          13410























              1














              I think the simplest solution is to use a left join:



              >>> print(A)
              a b c d
              0 1 2 3 4
              1 2 3 4 5
              2 4 4 5 6

              >>> print(B)

              x y z t
              0 1 20 30 40
              1 3 4 5 6

              >>> result = A.merge(left_on='a', right=B, right_on='x', how='left')[['z', 'd']].dropna()

              >>> print(result)
              z d
              0 30.0 4





              share|improve this answer



























                1














                I think the simplest solution is to use a left join:



                >>> print(A)
                a b c d
                0 1 2 3 4
                1 2 3 4 5
                2 4 4 5 6

                >>> print(B)

                x y z t
                0 1 20 30 40
                1 3 4 5 6

                >>> result = A.merge(left_on='a', right=B, right_on='x', how='left')[['z', 'd']].dropna()

                >>> print(result)
                z d
                0 30.0 4





                share|improve this answer

























                  1












                  1








                  1







                  I think the simplest solution is to use a left join:



                  >>> print(A)
                  a b c d
                  0 1 2 3 4
                  1 2 3 4 5
                  2 4 4 5 6

                  >>> print(B)

                  x y z t
                  0 1 20 30 40
                  1 3 4 5 6

                  >>> result = A.merge(left_on='a', right=B, right_on='x', how='left')[['z', 'd']].dropna()

                  >>> print(result)
                  z d
                  0 30.0 4





                  share|improve this answer













                  I think the simplest solution is to use a left join:



                  >>> print(A)
                  a b c d
                  0 1 2 3 4
                  1 2 3 4 5
                  2 4 4 5 6

                  >>> print(B)

                  x y z t
                  0 1 20 30 40
                  1 3 4 5 6

                  >>> result = A.merge(left_on='a', right=B, right_on='x', how='left')[['z', 'd']].dropna()

                  >>> print(result)
                  z d
                  0 30.0 4






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 14:03









                  chthonicdaemonchthonicdaemon

                  12.5k3147




                  12.5k3147



























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53321027%2fhow-to-join-csv-in-python-pandas-comparing-2-csv%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







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3

                      Museum of Modern and Contemporary Art of Trento and Rovereto