如何根据某一列的值来选择pandas的某些行?
select rows whose column value equals a scalar, some_value
, use ==
:
df.loc[df['column_name'] == some_value]
select rows whose column value is in an iterable, some_values
, use isin
:
df.loc[df['column_name'].isin(some_values)]
Combine multiple conditions with &
:
df.loc[(df['column_name'] == some_value) & df['other_column'].isin(some_values)]
select rows whose column value does not equal some_value
, use !=
:
df.loc[df['column_name'] != some_value]
isin
returns a boolean Series, so to select rows whose value is not in some_values
, negate the boolean Series using ~
:
df.loc[~df['column_name'].isin(some_values)]