我如何在pandas中对groupby使用sum()和count()(两者)

时间:2016-01-28 08:14:43

标签: python pandas

df=pandas.DataFrame(processed_data_format, columns=["file_name", "innings", "over","ball", "individual ball", "runs","batsman", "wicket_status","bowler_name","fielder_name"])  
df.groupby(['batsman'])['runs','ball'].sum()

通过使用这个我将得到像

的结果
a 30 29
b 4  1
c 10 15

我想得到名为filename 的计数列,其中包含上述代码的结果。最终结果应该是

a 30 29 2
b 4  1  1
c 10 15 2

1 个答案:

答案 0 :(得分:0)

df=pandas.DataFrame(processed_data_format, columns=["file_name", "innings", "over","ball", "individual ball", "runs","batsman", "wicket_status","bowler_name","fielder_name"])      
a = {'runs':['sum'],'ball':['sum'],'file_name':['nunique']}
t = df.groupby('batsman').agg(a)

不需要对此格式使用count()而不是使用nunique来获取唯一值的数量