Home > database >  How do I check how many rows exists in one column of the same value pandas?
How do I check how many rows exists in one column of the same value pandas?

Time:04-23

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?

CodePudding user response:

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')])

CodePudding user response:

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'])
  • Related