Home > Software engineering >  filter dataframe value in pandas
filter dataframe value in pandas

Time:10-21

Hi I was trying to extract only the no's from a columns in my df with this code:

(df['hello']=='No')

But it changes the values from the df and put it like a boolean values, I just want to value_count in that column the No's, but I'm not sure what I'm doing wrong.

enter image description here

CodePudding user response:

If you would like count

(df['hello']=='No').sum()

More like value_counts

df['hello'].value_counts().loc['No']

CodePudding user response:

You can actually query a DataFrame with .value_counts(). This will return an integer of how many times the query is True.

print(df['hello'].value_counts()['No'])

CodePudding user response:

Assume you are using pandas.DataFrame, if you want to get the subset of df rows with 'hello' column value 'No', use:

df[df['hello']=='No']

If you only want the column 'hello':

df['hello'][df['hello']=='No']

CodePudding user response:

print(df['hello'].value_counts())
  • Related