sample dataframes are
df1=
A B C
a0 b0 c0
a1 b1 c1
a2 b2 c2
...
an bn cn
df2=
A B
x0 y0
x1 y1
x2 y2
...
xm ym
What I want to replace is,
if (ai,bi) = (xj,yj) for some j, then ci = 0
For example,
df3=
A B C
1 4 c0
2 8 c1
2 9 c2
3 12 c3
3 16 c4
4 16 c5
df4=
A B
2 8
3 12
and I would like to get below
result=
A B C
1 4 c0
2 8 0
2 9 c2
3 12 0
3 16 c4
4 16 c5
I couldn't find how to replace by using np.where
. I need your help!
Using pd.concat
with fillna
and drop_duplicates
you can do:
result = (pd.concat([df4,df3],ignore_index=True,sort=False)
.fillna(0).drop_duplicates(['A','B'],keep='first'))
print (result)
A B C
0 2 8 0
1 3 12 0
0 1 4 c0
2 2 9 c2
4 3 16 c4
5 4 16 c5
you can add .sort_values(['A','B']).reset_index(drop=True)
at the end to get the exact same result
Using merge
with the parameter how='inner'
to get the index to change and then loc
these index:
result = df3.copy()
result.loc[df3.reset_index().merge(df4,how='inner')['index'],'C'] = 0
print (result)
A B C
0 1 4 c0
1 2 8 0
2 2 9 c2
3 3 12 0
4 3 16 c4
5 4 16 c5
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.