Joining dataframes by the columns with same namesHow to sort a dataframe by multiple column(s)Selecting multiple columns in a pandas dataframeRenaming columns in pandasAdding new column to existing DataFrame in Python pandasDelete column from pandas DataFrameChange data type of columns in PandasHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headersWhy is “1000000000000000 in range(1000000000000001)” so fast in Python 3?

Intelligent Ants in the Amazon

Why does wrapping Aluminium foil around my food help it keep warm, aluminium be good conductor should have no effect?

Why weren't bootable game disks ever a thing on the IBM PC?

Elf (adjective) vs. Elvish vs. Elven

Is it possible to split a vertex?

How quality assurance engineers test calculations?

How do native German speakers usually express skepticism (using even) about a premise?

Using Open with a filename that contains :

Could you brine steak?

A horrible Stockfish chess engine evaluation

Data Encryption by Application vs Data Encryption in Database

How to drill holes in 3/8" steel plates?

Why archangel Michael didn't save Jesus when he was crucified?

Misrepresented my work history

Is there any reason why MCU changed the Snap to Blip

What is the minimum time required for final wash in film development?

Through: how to use it with subtraction of functions?

What attributes and how big would a sea creature(s) need to be able to tow a ship?

LED glows slightly during soldering

Party going through airport security at separate times?

Should I include code in my research paper?

Is a request to book a business flight ticket for a graduate student an unreasonable one?

What's it called when the bad guy gets eaten?

What happens when adult Billy Batson says "Shazam"?



Joining dataframes by the columns with same names


How to sort a dataframe by multiple column(s)Selecting multiple columns in a pandas dataframeRenaming columns in pandasAdding new column to existing DataFrame in Python pandasDelete column from pandas DataFrameChange data type of columns in PandasHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headersWhy is “1000000000000000 in range(1000000000000001)” so fast in Python 3?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I have the following dataframes:



df:



A B C
1 x 1
2 y 2


and



df2:



A C D E
3 3 x l
4 4 z k


I want the following:



df_r:



A C
1 1
2 2
3 3
4 4


Note: This is just and example, the answer should be capable of not knowing first hand what are the same columns. i.e. Imagine you have a thousand columns.










share|improve this question






























    1















    I have the following dataframes:



    df:



    A B C
    1 x 1
    2 y 2


    and



    df2:



    A C D E
    3 3 x l
    4 4 z k


    I want the following:



    df_r:



    A C
    1 1
    2 2
    3 3
    4 4


    Note: This is just and example, the answer should be capable of not knowing first hand what are the same columns. i.e. Imagine you have a thousand columns.










    share|improve this question


























      1












      1








      1








      I have the following dataframes:



      df:



      A B C
      1 x 1
      2 y 2


      and



      df2:



      A C D E
      3 3 x l
      4 4 z k


      I want the following:



      df_r:



      A C
      1 1
      2 2
      3 3
      4 4


      Note: This is just and example, the answer should be capable of not knowing first hand what are the same columns. i.e. Imagine you have a thousand columns.










      share|improve this question
















      I have the following dataframes:



      df:



      A B C
      1 x 1
      2 y 2


      and



      df2:



      A C D E
      3 3 x l
      4 4 z k


      I want the following:



      df_r:



      A C
      1 1
      2 2
      3 3
      4 4


      Note: This is just and example, the answer should be capable of not knowing first hand what are the same columns. i.e. Imagine you have a thousand columns.







      python pandas dataframe






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 2 at 4:44







      Antonio López Ruiz

















      asked Mar 26 at 0:54









      Antonio López RuizAntonio López Ruiz

      4711 gold badge5 silver badges25 bronze badges




      4711 gold badge5 silver badges25 bronze badges






















          2 Answers
          2






          active

          oldest

          votes


















          3














          It is time to introduce concat with join



          pd.concat([df1,df2],join='inner',ignore_index =True)
          Out[30]:
          A C
          0 1 1
          1 2 2
          2 3 3
          3 4 4


          Another way using align



          pd.concat(df1.align(df2,join='inner',axis=1),ignore_index =True)
          Out[37]:
          A C
          0 1 1
          1 2 2
          2 3 3
          3 4 4



          Both of the methods working for outer and inner join for merge index or columns






          share|improve this answer

























          • Great answer, I like that it is so clean.

            – Antonio López Ruiz
            Mar 26 at 1:03






          • 1





            @AntonioLópezRuiz ha happy coding :-)

            – WeNYoBen
            Mar 26 at 1:03











          • @AntonioLópezRuiz I have updated another method , if you would like to check :-), the only weakness of this , is hard to manage more than 2 dataframe combine together .

            – WeNYoBen
            Mar 26 at 1:05












          • I checked it, I believe that the first method is the best one, since you can have theoretically an unlimited amount of dataframes and it should still work. That is also why I am choosing it as the correct answer, since it works for a lot of scenarios and it is really clean.

            – Antonio López Ruiz
            Mar 26 at 1:33



















          2














          Simple with pd.concat



          cols = set(df.columns).intersection(df2.columns)
          pd.concat([df[cols], df2[cols]])



          Also simple with df.append



          df[cols].append(df2[cols])





          share|improve this answer

























          • I mush be more specific. I am talking about thousands of columns, so I do not know the ones that are the same.

            – Antonio López Ruiz
            Mar 26 at 0:59






          • 1





            @AntonioLópezRuiz updated

            – rafaelc
            Mar 26 at 1:00






          • 1





            intersection is good fix :-)

            – WeNYoBen
            Mar 26 at 1:01






          • 1





            @Wen-Ben thanks! Nice concat+join btw ;)

            – rafaelc
            Mar 26 at 1: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',
          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%2f55348394%2fjoining-dataframes-by-the-columns-with-same-names%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









          3














          It is time to introduce concat with join



          pd.concat([df1,df2],join='inner',ignore_index =True)
          Out[30]:
          A C
          0 1 1
          1 2 2
          2 3 3
          3 4 4


          Another way using align



          pd.concat(df1.align(df2,join='inner',axis=1),ignore_index =True)
          Out[37]:
          A C
          0 1 1
          1 2 2
          2 3 3
          3 4 4



          Both of the methods working for outer and inner join for merge index or columns






          share|improve this answer

























          • Great answer, I like that it is so clean.

            – Antonio López Ruiz
            Mar 26 at 1:03






          • 1





            @AntonioLópezRuiz ha happy coding :-)

            – WeNYoBen
            Mar 26 at 1:03











          • @AntonioLópezRuiz I have updated another method , if you would like to check :-), the only weakness of this , is hard to manage more than 2 dataframe combine together .

            – WeNYoBen
            Mar 26 at 1:05












          • I checked it, I believe that the first method is the best one, since you can have theoretically an unlimited amount of dataframes and it should still work. That is also why I am choosing it as the correct answer, since it works for a lot of scenarios and it is really clean.

            – Antonio López Ruiz
            Mar 26 at 1:33
















          3














          It is time to introduce concat with join



          pd.concat([df1,df2],join='inner',ignore_index =True)
          Out[30]:
          A C
          0 1 1
          1 2 2
          2 3 3
          3 4 4


          Another way using align



          pd.concat(df1.align(df2,join='inner',axis=1),ignore_index =True)
          Out[37]:
          A C
          0 1 1
          1 2 2
          2 3 3
          3 4 4



          Both of the methods working for outer and inner join for merge index or columns






          share|improve this answer

























          • Great answer, I like that it is so clean.

            – Antonio López Ruiz
            Mar 26 at 1:03






          • 1





            @AntonioLópezRuiz ha happy coding :-)

            – WeNYoBen
            Mar 26 at 1:03











          • @AntonioLópezRuiz I have updated another method , if you would like to check :-), the only weakness of this , is hard to manage more than 2 dataframe combine together .

            – WeNYoBen
            Mar 26 at 1:05












          • I checked it, I believe that the first method is the best one, since you can have theoretically an unlimited amount of dataframes and it should still work. That is also why I am choosing it as the correct answer, since it works for a lot of scenarios and it is really clean.

            – Antonio López Ruiz
            Mar 26 at 1:33














          3












          3








          3







          It is time to introduce concat with join



          pd.concat([df1,df2],join='inner',ignore_index =True)
          Out[30]:
          A C
          0 1 1
          1 2 2
          2 3 3
          3 4 4


          Another way using align



          pd.concat(df1.align(df2,join='inner',axis=1),ignore_index =True)
          Out[37]:
          A C
          0 1 1
          1 2 2
          2 3 3
          3 4 4



          Both of the methods working for outer and inner join for merge index or columns






          share|improve this answer















          It is time to introduce concat with join



          pd.concat([df1,df2],join='inner',ignore_index =True)
          Out[30]:
          A C
          0 1 1
          1 2 2
          2 3 3
          3 4 4


          Another way using align



          pd.concat(df1.align(df2,join='inner',axis=1),ignore_index =True)
          Out[37]:
          A C
          0 1 1
          1 2 2
          2 3 3
          3 4 4



          Both of the methods working for outer and inner join for merge index or columns







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 26 at 1:04

























          answered Mar 26 at 1:00









          WeNYoBenWeNYoBen

          145k8 gold badges51 silver badges82 bronze badges




          145k8 gold badges51 silver badges82 bronze badges












          • Great answer, I like that it is so clean.

            – Antonio López Ruiz
            Mar 26 at 1:03






          • 1





            @AntonioLópezRuiz ha happy coding :-)

            – WeNYoBen
            Mar 26 at 1:03











          • @AntonioLópezRuiz I have updated another method , if you would like to check :-), the only weakness of this , is hard to manage more than 2 dataframe combine together .

            – WeNYoBen
            Mar 26 at 1:05












          • I checked it, I believe that the first method is the best one, since you can have theoretically an unlimited amount of dataframes and it should still work. That is also why I am choosing it as the correct answer, since it works for a lot of scenarios and it is really clean.

            – Antonio López Ruiz
            Mar 26 at 1:33


















          • Great answer, I like that it is so clean.

            – Antonio López Ruiz
            Mar 26 at 1:03






          • 1





            @AntonioLópezRuiz ha happy coding :-)

            – WeNYoBen
            Mar 26 at 1:03











          • @AntonioLópezRuiz I have updated another method , if you would like to check :-), the only weakness of this , is hard to manage more than 2 dataframe combine together .

            – WeNYoBen
            Mar 26 at 1:05












          • I checked it, I believe that the first method is the best one, since you can have theoretically an unlimited amount of dataframes and it should still work. That is also why I am choosing it as the correct answer, since it works for a lot of scenarios and it is really clean.

            – Antonio López Ruiz
            Mar 26 at 1:33

















          Great answer, I like that it is so clean.

          – Antonio López Ruiz
          Mar 26 at 1:03





          Great answer, I like that it is so clean.

          – Antonio López Ruiz
          Mar 26 at 1:03




          1




          1





          @AntonioLópezRuiz ha happy coding :-)

          – WeNYoBen
          Mar 26 at 1:03





          @AntonioLópezRuiz ha happy coding :-)

          – WeNYoBen
          Mar 26 at 1:03













          @AntonioLópezRuiz I have updated another method , if you would like to check :-), the only weakness of this , is hard to manage more than 2 dataframe combine together .

          – WeNYoBen
          Mar 26 at 1:05






          @AntonioLópezRuiz I have updated another method , if you would like to check :-), the only weakness of this , is hard to manage more than 2 dataframe combine together .

          – WeNYoBen
          Mar 26 at 1:05














          I checked it, I believe that the first method is the best one, since you can have theoretically an unlimited amount of dataframes and it should still work. That is also why I am choosing it as the correct answer, since it works for a lot of scenarios and it is really clean.

          – Antonio López Ruiz
          Mar 26 at 1:33






          I checked it, I believe that the first method is the best one, since you can have theoretically an unlimited amount of dataframes and it should still work. That is also why I am choosing it as the correct answer, since it works for a lot of scenarios and it is really clean.

          – Antonio López Ruiz
          Mar 26 at 1:33














          2














          Simple with pd.concat



          cols = set(df.columns).intersection(df2.columns)
          pd.concat([df[cols], df2[cols]])



          Also simple with df.append



          df[cols].append(df2[cols])





          share|improve this answer

























          • I mush be more specific. I am talking about thousands of columns, so I do not know the ones that are the same.

            – Antonio López Ruiz
            Mar 26 at 0:59






          • 1





            @AntonioLópezRuiz updated

            – rafaelc
            Mar 26 at 1:00






          • 1





            intersection is good fix :-)

            – WeNYoBen
            Mar 26 at 1:01






          • 1





            @Wen-Ben thanks! Nice concat+join btw ;)

            – rafaelc
            Mar 26 at 1:01















          2














          Simple with pd.concat



          cols = set(df.columns).intersection(df2.columns)
          pd.concat([df[cols], df2[cols]])



          Also simple with df.append



          df[cols].append(df2[cols])





          share|improve this answer

























          • I mush be more specific. I am talking about thousands of columns, so I do not know the ones that are the same.

            – Antonio López Ruiz
            Mar 26 at 0:59






          • 1





            @AntonioLópezRuiz updated

            – rafaelc
            Mar 26 at 1:00






          • 1





            intersection is good fix :-)

            – WeNYoBen
            Mar 26 at 1:01






          • 1





            @Wen-Ben thanks! Nice concat+join btw ;)

            – rafaelc
            Mar 26 at 1:01













          2












          2








          2







          Simple with pd.concat



          cols = set(df.columns).intersection(df2.columns)
          pd.concat([df[cols], df2[cols]])



          Also simple with df.append



          df[cols].append(df2[cols])





          share|improve this answer















          Simple with pd.concat



          cols = set(df.columns).intersection(df2.columns)
          pd.concat([df[cols], df2[cols]])



          Also simple with df.append



          df[cols].append(df2[cols])






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 26 at 1:00

























          answered Mar 26 at 0:56









          rafaelcrafaelc

          31.2k8 gold badges32 silver badges55 bronze badges




          31.2k8 gold badges32 silver badges55 bronze badges












          • I mush be more specific. I am talking about thousands of columns, so I do not know the ones that are the same.

            – Antonio López Ruiz
            Mar 26 at 0:59






          • 1





            @AntonioLópezRuiz updated

            – rafaelc
            Mar 26 at 1:00






          • 1





            intersection is good fix :-)

            – WeNYoBen
            Mar 26 at 1:01






          • 1





            @Wen-Ben thanks! Nice concat+join btw ;)

            – rafaelc
            Mar 26 at 1:01

















          • I mush be more specific. I am talking about thousands of columns, so I do not know the ones that are the same.

            – Antonio López Ruiz
            Mar 26 at 0:59






          • 1





            @AntonioLópezRuiz updated

            – rafaelc
            Mar 26 at 1:00






          • 1





            intersection is good fix :-)

            – WeNYoBen
            Mar 26 at 1:01






          • 1





            @Wen-Ben thanks! Nice concat+join btw ;)

            – rafaelc
            Mar 26 at 1:01
















          I mush be more specific. I am talking about thousands of columns, so I do not know the ones that are the same.

          – Antonio López Ruiz
          Mar 26 at 0:59





          I mush be more specific. I am talking about thousands of columns, so I do not know the ones that are the same.

          – Antonio López Ruiz
          Mar 26 at 0:59




          1




          1





          @AntonioLópezRuiz updated

          – rafaelc
          Mar 26 at 1:00





          @AntonioLópezRuiz updated

          – rafaelc
          Mar 26 at 1:00




          1




          1





          intersection is good fix :-)

          – WeNYoBen
          Mar 26 at 1:01





          intersection is good fix :-)

          – WeNYoBen
          Mar 26 at 1:01




          1




          1





          @Wen-Ben thanks! Nice concat+join btw ;)

          – rafaelc
          Mar 26 at 1:01





          @Wen-Ben thanks! Nice concat+join btw ;)

          – rafaelc
          Mar 26 at 1: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.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348394%2fjoining-dataframes-by-the-columns-with-same-names%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

          How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

          155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

          Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?