Chris Y
Chris Y

Reputation: 121

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

Upvotes: 0

Views: 52

Answers (4)

Yuca
Yuca

Reputation: 6091

Try this:

df_revised.join(df['C'])

Output:

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

Upvotes: 1

zipa
zipa

Reputation: 27879

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

Upvotes: 0

BENY
BENY

Reputation: 323306

Using update

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

Upvotes: 0

dheinz
dheinz

Reputation: 1096

You can use:

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

Upvotes: 1

Related Questions