[英]edit pandas dataframe fields based on key in grouping
Given this dataFrame: 给定此dataFrame:
import pandas as pd
a = pd.DataFrame({
'id': [1,2,3,4,5],
'company_id': [11,11,22,33,55],
'accSync': [True, False, False, False, True]
})
I need to modify all rows for a given company_id
based on the accSync
field being True, meaning that if any row for a given company_id
has a true in it, all rows for that company_id
need to be updated to be true as well. 我需要根据
accSync
字段将True修改为给定company_id
的所有行,这意味着如果给定company_id
任何行中都包含true,则该company_id
所有行也都必须更新为true。
In this case, company_id
11 has a True
in it and therefore row 2 should be updated to be 2, 11, True
as well while rows 1,3,4,5 should remain unaffected. 在这种情况下,
company_id
11中包含True
,因此第2行也应更新为2、11 2, 11, True
,而第1、3、4、5行应保持不受影响。
I tried using a combination if groupby
and any
but aren't getting anywhere. 我尝试使用组合,如果
groupby
和any
但没有得到任何结果。
IIUC transform
IIUC
transform
a.groupby('company_id')['accSync'].transform('max')
Out[131]:
0 True
1 True
2 False
3 False
4 True
Name: accSync, dtype: bool
Assign it back 分配回去
a['accSync']= a.groupby('company_id')['accSync'].transform('max')
def checker(df):
df["accSync"] = df["accSync"].any()
return df
new_df = a.groupby(by="company_id").apply(checker)
Result: 结果:
id company_id accSync
0 1 11 True
1 2 11 True
2 3 22 False
3 4 33 False
4 5 55 True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.