此问题在此处已有答案:
What is the most efficient way of counting occurrences in pandas?(4个回答)
26天前关闭。
我有这样一个框架:
dfsupport = pd.DataFrame({'Date': ['8/12/2020','8/12/2020','13/1/2020','24/5/2020','31/10/2020','11/7/2020','11/7/2020','4/4/2020','1/2/2020'],
'Category': ['Table','Chair','Cushion','Table','Chair','Mats','Mats','Large','Large'],
'Sales': ['1 table','3chairs','8 cushions','3Tables','12 Chairs','12Mats','4Mats','13 Chairs and 2 Tables', '3 mats, 2 cushions 4@chairs'],
'Paid': ['Yes','Yes','Yes','Yes','No','Yes','Yes','No','Yes'],
'Amount': ['93.78','$51.99','44.99','38.24','£29.99','29 21 only','18','312.8','63.77' ]
})
字符串
如果我想找到一个类别的示例数,这是最好的方法吗?
print(dfsupport.groupby(dfsupport['Category'],dropna=True).apply(lambda y: y['Category'].count()))
型
产出:
Category
Chair 2
Cushion 1
Large 2
Mats 2
Table 2
dtype: int64
型
1条答案
按热度按时间1cklez4t1#
你可以使用value_counts方法来获取一个序列,其中每个唯一值都有一个计数。在你的特定示例中,代码如下所示:
字符串