且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

根据另一个数据框python pandas替换列值-更好的方法吗?

更新时间:2022-12-09 16:15:57

使用

Use the boolean mask from isin to filter the df and assign the desired row values from the rhs df:

In [27]:

df.loc[df.Name.isin(df1.Name), ['Nonprofit', 'Education']] = df1[['Nonprofit', 'Education']]
df
Out[27]:
  Name  Nonprofit  Business  Education
0    X          1         1          0
1    Y          1         1          1
2    Z          1         0          1
3    Y          1         1          1

[4 rows x 4 columns]