'How do I check how many rows exists in one column of the same value pandas?
I have a pandas dataset of about 200k articles containing a column called Category. How do I display all the different types of articles and also count the number of rows a certain category for example "Entertainment" exists in the Category column?
Solution 1:[1]
To get the different Category :
df['Category'].unique()
And the following to count the number of rows using contains for the category Entertainment :
len(df[df['Category'].str.contains('Entertainment')])
Solution 2:[2]
Use Series.value_counts, then is possible see unique Category values in index and for count select values by Series.loc:
s = df['Category'].value_counts()
print (s.index.tolist())
print (s.loc['Entertainment'])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | tlentali |
| Solution 2 | jezrael |
