Issue
I have this dataframe:
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' ]
})
If I want to find the number of instances of a category is this the best way to do it?
print(dfsupport.groupby(dfsupport['Category'],dropna=True).apply(lambda y: y['Category'].count()))
output:
Category
Chair 2
Cushion 1
Large 2
Mats 2
Table 2
dtype: int64
Solution
You can instead use the value_counts method to get a series with a count of each unique value. In your specific example the code would look like this:
dfsupport["Category"].value_counts()
Answered By - JohnnyPro
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.