简体   繁体   中英

Pandas (Python) - Update column of a dataframe from another one with conditions

I had a problem and I found a solution but I feel it's the wrong way to do it. Maybe, there is a more 'canonical' way to do it.

Problem

I have two dataframe that I would like to merge without having extra column and without erasing existing infos. Example :

Existing dataframe (df)

   A  A2  B
0  1   4  0
1  2   5  1

Dataframe to merge (df2)

   A  A2  B
0  1   4  2
1  3   5  2

I would like to update df with df2 if columns 'A' and 'A2' corresponds. The result would be (:

   A  A2    B
0  1   4  2.0 <= Update value ONLY
1  2   5  1.0

Here is my solution, but I think it's not a really good one.

import pandas as pd

df = pd.DataFrame([[1,4,0],[2,5,1]],columns=['A','A2','B'])

df2 = pd.DataFrame([[1,4,2],[3,5,2]],columns=['A','A2','B'])

df = df.merge(df2,on=['A', 'A2'],how='left')
df['B_y'].fillna(0, inplace=True)
df['B'] = df['B_x']+df['B_y']
df = df.drop(['B_x','B_y'], axis=1)
print(df)

Does anyone has a better way to do ? Thanks !

Yes, it can be done without merge:

rows = (df[['A','A2']] == df2[['A','A2']]).all(axis=1)
df.loc[rows,'B'] = df2.loc[rows,'B']

You can try this:

df.ix[df2.loc[(df['A'] == df2['A']) & (df['A2'] ==   
df2['A2']),'B'].index.values,'B'] = \
df2.loc[(df['A'] == df2['A']) & (df['A2'] == df2['A2']),'B']

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