[英]Groupby count of values - pandas
I'm hoping to count specific values from a pandas df.我希望从 pandas df 中计算特定值。 Using below, I'm subsetting Item
by Up
and grouping Num
and Label
to count the values in Item
.使用下面,我通过Up
对Item
进行子集化,并将Num
和Label
分组以计算Item
中的值。 The values in the output are correct but I want to drop Label
and include Up
in the column headers. output 中的值是正确的,但我想删除Label
并在列标题中包含Up
。
import pandas as pd
df = pd.DataFrame({
'Num' : [1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2],
'Label' : ['A','B','A','B','B','B','A','B','B','A','A','A','B','A','B','A'],
'Item' : ['Up','Left','Up','Left','Down','Right','Up','Down','Right','Down','Right','Up','Up','Right','Down','Left'],
})
df1 = (df[df['Item'] == 'Up']
.groupby(['Num','Label'])['Item']
.count()
.unstack(fill_value = 0)
.reset_index()
)
intended output:预期 output:
Num A_Up B_Up
1 3 0
2 1 1
With your approach, you can include the Item in the grouper.通过您的方法,您可以将 Item 包含在 grouper 中。
out = (df[df['Item'] == 'Up'].groupby(['Num','Label','Item']).size()
.unstack(['Label','Item'],fill_value=0))
out.columns=out.columns.map('_'.join)
print(out)
A_Up B_Up
Num
1 3 0
2 1 1
You can use Groupby.transform
to have all column names.您可以使用Groupby.transform
来拥有所有列名。 Then use df.pivot_table
and a list comprehension
to get your desired column names.然后使用df.pivot_table
和list comprehension
来获取您想要的列名。
In [2301]: x = df[df['Item'] == 'Up']
In [2304]: x['c'] = x.groupby(['Num','Label'])['Item'].transform('count')
In [2310]: x = x.pivot_table(index='Num', columns=['Label', 'Item'], aggfunc='first', fill_value=0)
In [2313]: x.columns = [j+'_'+k for i,j,k in x.columns]
In [2314]: x
Out[2314]:
A_Up B_Up
Num
1 3 0
2 1 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.