Skip to content
Home » How to select rows from a DataFrame based on column values

How to select rows from a DataFrame based on column values

There are multiple ways to do select rows from a pandas DataFrame based on the column names and values. Below are few mostly used ways:

Boolean Indexing

Finding the true value of each row’s 'A' column being equal to 'run' is required for Boolean indexing, and then using those truth values to determine which rows to keep.

import numpy as np
import pandas as pd

#pandas Dataframe
df = pd.DataFrame({'A': 'run code code run run run code'.split(),
                   'B': 'bar too too bar bar too too'.split(),
                   'C': np.arange(7)*10, 'D': np.arange(7) })

out = df['A'] == 'run'
df[out]

Position Indexing

If you want to select rows based on single value.

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

Example:

df.loc[df['A'] == 'run']

If you wan to select rows based on list of values:

df.loc[df['column_name'].isin(list_of_values)]

Example:

df.loc[df['A'].isin(['run','code'])]

Using df.query()

df.query('A == "run"')