简体   繁体   English

如何在大熊猫的group_by中计算特定值?

[英]How can I count a specific value in group_by in pandas?

I have a dataframe and I use groupby to group it by Season. 我有一个数据框,我使用groupby按季节对其进行分组。 One of the columns of the original df is named Check and consists of True and False. 原始df的一列名为Check,由True和False组成。 My aim it to count the True values for each group and put it in the new dataframe. 我的目标是计算每个组的True值并将其放入新的数据框中。

import pandas as pd

df = ....
df['Check'] = df['Actual'] == df['Prediction']
grouped_per_year = df.groupby('Season')

df_2= pd.DataFrame()
df_2['Seasons'] = total_matches_per_year.keys()
df_2['Successes'] = ''
df_2['Total_Matches'] = list(grouped_per_year.size())
df_2['SR'] = df_2['Successes'] / df_2['Total_Matches']
df_2['Money_In'] = list(grouped_per_year['Money_In'].apply(sum))
df_2['Profit (%)'] = (df_profit['Money_In'] - df_profit['Total_Matches']) / df_profit['Total_Matches'] * 100.

I have tried: 我努力了:

successes_per_year = grouped_per_year['Pred_Check'].value_counts()

but I don't know how to get only the True count. 但是我不知道如何只获得真实的计数。

为了计算True ,您还可以使用sum (在执行数字运算时为True = 1和False = 0):

grouped_per_year['Pred_Check'].sum()

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

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