Pandas DataFrame - Filter Rows


Pandas DataFrame - Filter Rows

To filter rows of Pandas DataFrame, you can use DataFrame.isin() function. isin() returns a dataframe of boolean which when used with the original dataframe, filters rows that obey the filter criteria.

You can also use DataFrame.query() to filter out the rows that satisfy a given boolean expression.

In this tutorial, we shall learn how to filter rows of a DataFrame based on a condition applied on the column values.

Examples

1. Filter DataFrame rows using DataFrame.isin()

In this example, we shall take a DataFrame with two columns named a and b and four rows. We shall filter this DataFrame, based on the condition that the values of column a lies in a given range.

Python Program

import pandas as pd

#initialize dataframe
df = pd.DataFrame({'a': [2, 4, 8, 5], 'b': [2, 0, 9, 7]})

#check if the values of df['a'] are in the range(3,6)
out = df['a'].isin(range(3,6))

#filter dataframe
filtered_df = df[out]

print('Original DataFrame\n-------------------\n',df)
print('\nFiltered DataFrame\n-------------------\n',filtered_df)

isin() function returns True for the rows whose column a values are in the range(3,6). Else the function returns false.

df[out] returns only those rows whose value is True, thus resulting in the filtered output.

Output

Original DataFrame
-------------------
    a  b
0  2  2
1  4  0
2  8  9
3  5  7

Filtered DataFrame
-------------------
    a  b
1  4  0
3  5  7

2. Filter DataFrame rows using DataFrame.query()

In this example, we shall initialize a DataFrame with two columns a and b, containing four rows. We shall filter those rows whose column b value is greater than 4.

We shall use DataFrame.query() to filter the DataFrame rows.

Python Program

import pandas as pd

#initialize dataframe
df = pd.DataFrame({'a': [1, 4, 7, 2], 'b': [2, 0, 8, 7]})

#get rows where that b>4
filtered_df = df.query('b>4')

print('Original DataFrame\n-------------------\n',df)
print('\nFiltered DataFrame\n-------------------\n',filtered_df)

Output

Original DataFrame
-------------------
    a  b
0  1  2
1  4  0
2  7  8
3  2  7

Filtered DataFrame
-------------------
    a  b
2  7  8
3  2  7

Summary

In this tutorial of Python Examples, we learned how to filter a DataFrame, based on the conditions applied on the values of its columns.