简体   繁体   English

自定义 groupby 函数 pandas python

[英]custom groupby function pandas python

I have a following dataframe:我有以下数据框:

在此处输入图像描述

I would like to group by id and add a flag column which contains Y if anytime Y has occurred against id, resultant DF would like following:我想按 id 分组并添加一个包含 Y 的标志列,如果任何时候 Y 发生在 id 上,结果 DF 会如下所示:

在此处输入图像描述

Here is my approach which is too time consuming and not sure of correctness:这是我的方法太耗时且不确定正确性:

temp=pd.DataFrame()
j='flag'
for i in df['id'].unique():
  test=df[df['id']==i]
  test[j]=np.where(np.any((test[j]=='Y')),'Y',test[j])
temp=temp.append(test)

Compare flag to Y , group by id , and use any :flagY进行比较,按id分组,并使用any

new_df = (df['flag'] == 'Y').groupby(df['id']).any().map({True:'Y', False:'N'}).reset_index()

Output:输出:

>>> new_df
   id flag
0   1    Y
1   2    Y
2   3    N
3   4    N
4   5    Y

You can do groupby + max since Y > N :你可以做groupby + max因为Y > N

df.groupby('id', as_index=False)['flag'].max()

   id flag
0   1    Y
1   2    Y
2   3    N
3   4    N
4   5    Y

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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