简体   繁体   中英

Python Pandas Merge and Append Data

I'm trying to merge data from 2 dataframes where df_revised updates values for same column/row, but then also keep columns from df that didn't exist in df_revised and struggling to accomplish this. PARID should be the index. (There will never be PARIDs in one df different from another.)

df

PARID   A   B   C
100     2   3   99
101     1   3   84  

df_revised

PARID   A   B
100     33  44
101     10  33

Desired Output

PARID   A   B   C
100     33  44  99
101     10  33  84

Try this:

df_revised.join(df['C'])

Output:

PARID   A   B   C            
100    33  44  99
101    10  33  84

You can use:

C = df[df['PARID','C']]
df_revised = df_revised.merge(C,on = 'PARID')

Using update

df1.update(df2)
df1
Out[64]: 
        A   B   C
PARID            
100    33  44  99
101    10  33  84

Using merge() you can do it with:

df_revised.merge(df[['PARID','C']], on='PARID')

#    A   B  PARID   C
#0  33  44    100  99
#1  10  33    101  84

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