简体   繁体   English

根据分组中的键编辑pandas数据框字段

[英]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. 我尝试使用组合,如果groupbyany但没有得到任何结果。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM