Source From Here
Question
How to select rows from a DataFrame based on values in some column in pandas? In SQL I would use:
I tried to look at pandas documentation but did not immediately find the answer.
How-To
To select rows whose column value equals a scalar, some_value, use == (Indexing and Selecting Data):
To select rows whose column value is in an iterable, some_values, use
isin:
Combine multiple conditions with
&:
To select rows whose column value does not equal some_value, use
!=:
isin returns a boolean Series, so to select rows whose value is not in some_values, negate the boolean Series using ~:
For example,
If you have multiple values you want to include, put them in a list (or more generally, any iterable) and use isin:
Note, however, that if you wish to do this many times, it is more efficient to make an index first, and then use DataFrame.loc:
Question
How to select rows from a DataFrame based on values in some column in pandas? In SQL I would use:
- select * from table where colume_name = some_value.
How-To
To select rows whose column value equals a scalar, some_value, use == (Indexing and Selecting Data):
- df.loc[df['column_name'] == some_value]
- df.loc[df['column_name'].isin(some_values)]
- df.loc[(df['column_name'] == some_value) & df['other_column'].isin(some_values)]
- df.loc[df['column_name'] != some_value]
- df.loc[~df['column_name'].isin(some_values)]
If you have multiple values you want to include, put them in a list (or more generally, any iterable) and use isin:
Note, however, that if you wish to do this many times, it is more efficient to make an index first, and then use DataFrame.loc:
B
one foo 0 0
one bar 1 2
two foo 2 4
three bar 3 6
two foo 4 8
two bar 5 10
one foo 6 12
three foo 7 14>
>>> print(df.loc['one']) // Select row(s) with column B as value 'one'
A C D
B
one foo 0 0
one bar 1 2
one foo 6 12
沒有留言:
張貼留言