Menu Close

How to select rows from a DataFrame based on column values with Python Pandas?

To select rows from a DataFrame based on column values with Python Pandas, we can use the loc property.

For instance, we write

df.loc[df['column_name'] == some_value]

to use the df.loc fictionary to get the column value with the df['column_name'] == some_value scalar.

We can combine conditions with & or |.

For instance, we write

df.loc[(df['column_name'] >= A) & (df['column_name'] <= B)]

to return the column values satifying df['column_name'] >= A and df['column_name'] <= B.

Posted in Python, Python Answers