Categories
Python Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *