[英]How to in-place update values in multiple columns in a DataFrame on condition from another using assignment operator?
There is one DataFrame S
to be updated: 有一个要更新的DataFrame
S
:
n ii a b c
0 True 10 11 1.20
1 False 2 0 NaN
2 True 34 75 2.14
3 True 22 88 0.02
from another DataFrame T
with another set of columns 从另一个带有另一组列的DataFrame
T
a b c
8 13 1.19
31 72 2.10
20 83 0.05
Is it possible to have S
updated in a function with one line assignment statement? 是否可以使用一个行分配语句在一个函数中更新
S
?
def process(S):
ii = S.ii
# ... internal calculations that produce T
columns = ['a', 'b', 'c']
S[ii][columns] = T[columns] # < ----- in-place update
That process
works on a pass-by-reference approach leaving S
updated after a call 该
process
适用于按引用传递方法,使呼叫后S
更新
process(S)
Try returning the dataframe: 尝试返回数据框:
def process(S):
ii = S.ii
# ... internal calculations that produce T
columns = ['a', 'b', 'c']
S.loc[ii, columns] = T[columns] # < ----- in-place update
return S
And call the function like this: 并像这样调用函数:
S = process(S)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.