简体   繁体   中英

how to merge or join pandas pivot table with python

I try to merge or join two pandas's pivot table t tried using merge and it does works but the result wasn't i expecting tobe, its giving me duplicate for either table

df1.fillna('', inplace=True)
df1.reset_index(inplace=True)
df2.fillna('', inplace=True)
df2.reset_index(inplace=True)
df = pd.merge(df1, df2, how='left', on=['KEY1', 'KEY2'])

First Pivot Table:

KEY1    KEY2    KEY3    Column_A0   Column_A1   Column_A2
Row_X0  Row_Y0  Row_Z0  123                     123
        Row_Y1  Row_Z0               456    
                Row_Z1  789                     789
Row_X1  Row_Y0  Row_Z0               123    
                Row_Z1  789     
                Row_Z2  456          789    

Second Pivot Table:

KEY1    KEY2    KEY3    Column_B0   Column_B1   Column_B2   Column_B3
Row_X0  Row_Y0  Row_W0  1           234     
                Row_W1  2           345     
                Row_W2  3           456     
        Row_Y1  Row_W0  4           567         1       234
                Row_W1  7           890         2       345
                Row_W2  8           901         3       456
                Row_W3  9           12          4       567
Row_X1  Row_Y0  Row_W0                          7       890
                Row_W1                          8       901
                Row_W2                          9       12

The result I expect:

KEY1    KEY2    KEY3_X   Column_A0  Column_A1   Column_A2   KEY3_Y  Column_B0   Column_B1   Column_B2   Column_B3
Row_X0  Row_Y0  Row_Z0   123                    123         Row_W0  1           234     
                                                            Row_W1  2           345     
                                                            Row_W2  3           456     
        Row_Y1  Row_Z0              456                     Row_W0  4           567         1            234
                Row_Z1   789                    789         Row_W1  7           890         2            345
                                                            Row_W2  8           901         3            456
                                                            Row_W3  9           12          4            567
                                                            Row_W0                          7            890
Row_X1  Row_Y0  Row_Z0              123                     Row_W1                          8            901
                Row_Z1   789                                Row_W2                          9            12
                Row_Z2   456                    789                     

is there any i can do to make this happen? thank you

Concat() by row or column. The pd.concat function allows you to tables using the column and or row

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