[英]How to in-place update values in multiple columns in a DataFrame on condition from another using assignment operator?
有一個要更新的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
從另一個帶有另一組列的DataFrame T
a b c
8 13 1.19
31 72 2.10
20 83 0.05
是否可以使用一個行分配語句在一個函數中更新S
?
def process(S):
ii = S.ii
# ... internal calculations that produce T
columns = ['a', 'b', 'c']
S[ii][columns] = T[columns] # < ----- in-place update
該process
適用於按引用傳遞方法,使呼叫后S
更新
process(S)
嘗試返回數據框:
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
並像這樣調用函數:
S = process(S)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.