根据条件从另一个df获取价值

时间:2020-10-02 12:14:29

标签: pandas numpy dataframe

我有2 df

df1:

ID       X Y Cond
Johnson  2 3 fine
Sand     NAN NAN sick
Cooper   1 2 fine
Nelson   1 2 fine
Peterson 4 5 fine

和df2:

id2      X Y 
Magic    2 3 
Sand     2 3 
Cooper   1 2 
Dean     1 2 

如果Cond =“ sick”和df [“ id”] = df [“ id2],我想更新df1中的x值 获得新的df1:

ID       X Y Cond
Johnson  2 3 fine
Sand     2 3  sick
Cooper   1 2 fine
Nelson   1 2 fine
Peterson 4 5 fine

我尝试过:

df1["x"] = np.where((df["cond"]=="sick")& (df1["id"]==df2["id2"]),df2["x"],"")

但是它不起作用。我得到这个ValueError:

ValueError: Can only compare identically-labeled Series objects

谢谢

2 个答案:

答案 0 :(得分:1)

首先将两个ID列都转换为索引值,以使DataFrame.loc可能匹配选定的行:

df11 = df1.set_index('ID')
df22 = df2.set_index('id2')

df11.loc[df11["Cond"]=="sick", ['X','Y']] = df22[['X','Y']]
df = df11.reset_index()
print (df)
         ID  X  Y  Cond
0   Johnson  2  3  fine
1      Sand  2  3  sick
2    Cooper  1  2  fine
3    Nelson  1  2  fine
4  Peterson  4  5  fine

答案 1 :(得分:0)

您可以使用pandas数据框的where()方法来代替numpy的where函数。代码如下:

df1.loc[:,["X", "Y"]] = df1.loc[:,["X", "Y"]].where(df1["Cond"]!="sick",df2.loc[:,["X", "Y"]])
相关问题