简体   繁体   中英

Concatenating dataframes on a common index

I'm currently writing a program, and have two dataframes, indexed by strings, with the following format:

         col1   col2   col3   col4
row1     65      24     47     35
row2     33      48     25     89
row3     65      34     67     34
row4     24      12     52     17

and

         col5   col6
row1     81     58
row2     25     36
row3     67     70
row4     52     82

and would like to merge/join/concatenate the frames into something which looks like this:

         col1   col2   col3   col4   col5   col6
row1     65      24     47     35    81     58
row2     33      48     25     89    25     36
row3     65      34     67     34    67     70
row4     24      12     52     17    52     82

With every method i've tried, and after reading through the Pandas documentation on merging/concatenation/joining, I was unable to find a way to perform such a merge without duplicate row indices, and usually the operations produced something looking like this:

         col1   col2   col3   col4  col5   col6
row1     65      24     47     35
row2     33      48     25     89
row3     65      34     67     34
row4     24      12     52     17
row1                                 81     58
row2                                 25     36
row3                                 67     70
row4                                 52     82

However, this is not the format I want my data in. What would be the most efficient way to perform a merge, so that values with identical indices are merged together? Note that the dataframes may be of different dimensions as well in some cases.

pd.concat along the first axis

pd.concat([df1, df2], 1)

      col1  col2  col3  col4  col5  col6
row1    65    24    47    35    81    58
row2    33    48    25    89    25    36
row3    65    34    67    34    67    70
row4    24    12    52    17    52    82

If the problem is with your index, you can add a parameter ignore_index=True :

df = pd.concat([df1, df2], 1, ignore_index=True)
df
       0   1   2   3   4   5
row1  65  24  47  35  81  58
row2  33  48  25  89  25  36
row3  65  34  67  34  67  70
row4  24  12  52  17  52  82

DataFrame.align

Another option,

df3, df4 = df1.align(df2)    
df3.fillna(0) + df4.fillna(0)

      col1  col2  col3  col4  col5  col6
row1  65.0  24.0  47.0  35.0  81.0  58.0
row2  33.0  48.0  25.0  89.0  25.0  36.0
row3  65.0  34.0  67.0  34.0  67.0  70.0
row4  24.0  12.0  52.0  17.0  52.0  82.0

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