filter dataframe value in pandas
Question:
Answers:
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']
If you would like count
(df['hello']=='No').sum()
More like value_counts
df['hello'].value_counts().loc['No']
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'])
print(df['hello'].value_counts())
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']
If you would like count
(df['hello']=='No').sum()
More like value_counts
df['hello'].value_counts().loc['No']
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'])
print(df['hello'].value_counts())