我有两个数据框:
print df1
name value a value b recommendation
a Arn 1 30 Up
b Bob 1 50 Wait
c Cain 2 70 Up
d Dain 3 80 Wait
print df2
name value a value b recommendation
a Arn 3 50 Up
b Bob 2 90 Up
c Cain 4 170 Up
d Dain 9 280 Up
我正在尝试使用df2值更新df1,建议使用更新方法在df1上“等待”:
df1.update(df2, filter_func = df1.loc[df1['recommendation'] == 'Wait'])
这不起作用。知道如何解决这个问题吗?
编辑
预期输出为:
print df1
name value a value b recommendation
a Arn 1 30 Up
b Bob 2 90 Up
c Cain 2 70 Up
d Dain 9 280 Up
答案 0 :(得分:0)
使用np.nan
,将其全部转到combine_first
,然后我们使用df1=df1.mask(df1=='Wait').combine_first(df2)
df1
Out[621]:
name valuea valueb recommendation
a Arn 1 30 Up
b Bob 1 50 Up
c Cain 2 70 Up
d Dain 3 80 Up
df1.loc[(df1.recommendation!='Wait')].combine_first(df2)
Out[623]:
name valuea valueb recommendation
a Arn 1.0 30.0 Up
b Bob 2.0 90.0 Up
c Cain 2.0 70.0 Up
d Dain 9.0 280.0 Up
如果您需要更新整行
Completable