简体   繁体   中英

Joining two pandas dataframe with different number of rows and columns

I have two pandas dataframe, df1 and df2. df1 has monthly time index, df2 has quarterly time index. df1 and df2 has different number of rows and columns, I want to: (1)join them side by side and keep their indexes. (2) output to excel file.

I have tried pd.concat, but this method concatenate dataframes based on one of the index of the dataframe which I don't want

df1:           df2:

   A   B        C  D  E
1  a   b    1   f  s  l
2  c   d    4   k  k  u
            5   y  y  j

Expected results in excel:

   A   B        C  D  E
1  a   b    1   f  s  l
2  c   d    4   k  k  u
            5   y  y  j

Since pd.concat matches the index, you have to reset your index before using the method:

pd.concat([df1.reset_index(drop=True), df2.reset_index(drop=True)], axis=1)

     A    B  C  D  E
0    a    b  f  s  l
1    c    d  k  k  u
2  NaN  NaN  y  y  j

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