简体   繁体   中英

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

I have the following 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:

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

Basically in the table above i am hoping for it to show 4 for every column (4 scores greater than 1)

Any help would be much appreciated! Thanks

We should do

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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