小编Jer*_*y Z的帖子

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

注意:为了简单起见,我正在使用一个玩具示例,因为复制/粘贴数据帧很难在堆栈溢出(请告诉我是否有一个简单的方法来执行此操作).

有没有办法将一个数据框中的值合并到另一个数据框而不获取_X,_Y列?我希望一列上的值替换另一列的所有零值.

df1: 

Name   Nonprofit    Business    Education

X      1             1           0
Y      0             1           0   <- Y and Z have zero values for Nonprofit and Educ
Z      0             0           0
Y      0             1           0

df2:

Name   Nonprofit    Education
Y       1            1     <- this df has the correct values. 
Z       1            1



pd.merge(df1, df2, on='Name', how='outer')

Name   Nonprofit_X    Business    Education_X     Nonprofit_Y     Education_Y
Y       1                1          1                1               1
Y      1                 1          1                1               1
X      1                 1          0               nan             nan   
Z …
Run Code Online (Sandbox Code Playgroud)

python pandas

16
推荐指数
4
解决办法
3万
查看次数

标签 统计

pandas ×1

python ×1