簡體   English   中英

如何使用 pandas 中的條件執行多個 groupby 和轉換計數

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

這是這里問題的延伸: 這里

我正在嘗試向 grouby 添加一個額外的列:

# 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','Other'])['Attempts'].transform('count')
df

在此處輸入圖像描述

然后我嘗試為分數大於 1(應該等於 4)的行的總和添加另一列:

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

但我得到了一個

ValueError: Grouper for '<class 'pandas.core.frame.DataFrame'>' not 1-dimensional

有任何想法嗎? 非常感謝!

df['Score'].gt(1)返回 boolean 系列,而不是 dataframe。 您需要先返回 dataframe 才能按相關列進行分組。

利用:

df = df[df['Score'].gt(1)]
df['scoregreaterthan1'] = df.groupby(['Rating','Other'])['Score'].transform('count')
df

output:

    Name    Attempts    Score   Category    Rating  Other   AttemptsbyRating    scoregreaterthan1
0   tom     10          2       c           100     x       6                4
1   tom     16          3       a           100     x       6                4
2   tom     22          2       a           100     x       6                4
4   matt    15          5       b           100     x       6                4

如果您想保留分數不大於 1 的人,請不要這樣做:

df = df[df['Score'].gt(1)]
df['scoregreaterthan1'] = df.groupby(['Rating','Other'])['Score'].transform('count')

做這個:

df['scoregreaterthan1'] = df[df['Score'].gt(1)].groupby(['Rating','Other'])['Score'].transform('count')
df['scoregreaterthan1'] = df['scoregreaterthan1'].ffill().astype(int)

output 2:

    Name    Attempts    Score   Category    Rating  Other   AttemptsbyRating    scoregreaterthan1
0   tom     10  2   c   100 x   6   4
1   tom     16  3   a   100 x   6   4
2   tom     22  2   a   100 x   6   4
3   matt    10  1   c   100 x   6   4
4   matt    15  5   b   100 x   6   4
5   matt    14  1   b   100 x   6   4

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM