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

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현