简体   繁体   English

如何使用 pandas 中的条件执行 groupby 和转换计数

[英]How to perform a groupby and transform count with a condition in pandas

I have the following dataframe:我有以下 dataframe:

# Import pandas library 
import pandas as pd
import numpy as np

# data
data = [['tom', 10,2,'c',100,'x'], ['tom',16 ,3,'a',100,'x'], ['tom', 22,2,'a',100,'x'],
        ['matt', 10,1,'c',100,'x'], ['matt', 15,5,'b',100,'x'], ['matt', 14,1,'b',100,'x']]

# Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['Name', 'Attempts','Score','Category','Rating','Other'])
df['AttemptsbyRating'] = df.groupby(by=['Rating'])['Attempts'].transform('count')
df

在此处输入图像描述

And i am then trying to create to extra columns - one showing the count of Attempts grouped by rating (as shown above works) and then trying to do another where i want to count scores greater than 1. I have tried:然后我尝试创建额外的列 - 一个显示按评级分组的尝试计数(如上所示),然后尝试做另一个我想计算大于 1 的分数。我试过:

df['scoregreaterthan1'] = df[df.groupby(by=['Rating'])['Score'].transform('count')>1]

And i am getting a ValueError: Wrong number of items passed 7, placement implies 1我得到一个ValueError: Wrong number of items passed 7, placement implies 1

Basically in the table above i am hoping for it to show 4 for every column (4 scores greater than 1)基本上在上表中,我希望它每列显示 4 个(4 个分数大于 1)

Any help would be much appreciated!任何帮助将非常感激! Thanks谢谢

We should do我们应该做

df['scoregreaterthan1'] = df['Score'].gt(1).groupby(df['Rating']).transform('sum')

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

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