简体   繁体   中英

How to merge two data frame by column names python

I have two data frame, one looks like this (shape: 12553*83):

    A12D        A131          A12B ...
0   1.096131    2.609943    -0.659828
1   1.111370    2.650422    -0.648742

...

Another looks like this (shape: 1*105)

            A12B        A0CM        A18V...
PAM50 mRNA  Basal-like  Basal-like  Basal-like

I want inner merge two table by same column name. Something like:

            A12D        A131          A12B ...
PAM50 mRNA  Basal-like  Basal-like  Basal-like
1           1.096131    2.609943    -0.659828
2           1.111370    2.650422    -0.648742

Since both two table are large, column names are not completely shown. Another problem is that column names may be duplicate. I have searched serval merge methods, most of them are just merge by column value. So what can I do in this case? Thanks in advance!

As @sushanth noted, use pd.concat() -- with join='inner' . Here is an example:

import pandas as pd
df1 = pd.DataFrame({'a': [1, 2, 3], 
                    'b': [4, 5, 6], 
                    'c': [7, 8, 9]})
df2 = pd.DataFrame({'b': [11, 12, 13], 
                    'c': [14, 15, 16], 
                    'd': [17, 18, 19]})
t = pd.concat([df1, df2], axis=0, join='inner')
print(t)

    b   c
0   4   7
1   5   8
2   6   9
0  11  14
1  12  15
2  13  16

More info here:

Okay problems solved, Thanks to everyone's kind help. I first removed duplicate columns, then concated two tables. Like

df1 = df1.loc[:,~df1.columns.duplicated()]

merged = pd.concat([df1,df2],join='inner')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM