How do you filter pandas DataFrames by multiple columns?

Given a Pandas DataFrame, we have to filter it by multiple columns.
Submitted by Pranit Sharma, on June 23, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and the data.

Filtering is a process of selecting some sort of information according to our specific demands. Filtering pandas is important for learning about the properties of specific elements and hence results in effective data analysis.

Filter pandas DataFrames by multiple columns

To filter pandas DataFrame by multiple columns, we simply compare that column values against a specific condition but when it comes to filtering of DataFrame by multiple columns, we need to use the AND (&&) Operator to match multiple columns with multiple conditions.

To work with pandas, we need to import pandas package first, below is the syntax:

import pandas as pd

Let us understand with the help of an example,

Python program to filter pandas DataFrames by multiple columns

# Importing pandas package
import pandas as pd

# Creating a dictionary
d= {
    'Product':['TV','Mobile','Fridge','Washing-Machine','TV','Mobile','Fridge','Washing-Machine'],
    'Month':['January','January','January','January','February','February','February','February'],
    'Sales':[10000,40000,22000,37000,19000,80000,67000,34000]
}

# Create a DataFrame
df = pd.DataFrame(d)

# Display original DataFrame
print("Original DataFrame:\n",df,"\n")

# Filtering Sales of washing machine in the month of February
result = df[(df['Product']=='Fridge') & (df['Month']=='February')]

# Display result
print("Filtered Data:\n",result)

Output

The output of the above program is:

Original DataFrame:
            Product     Month  Sales
0               TV   January  10000
1           Mobile   January  40000
2           Fridge   January  22000
3  Washing-Machine   January  37000
4               TV  February  19000
5           Mobile  February  80000
6           Fridge  February  67000
7  Washing-Machine  February  34000 

Filtered Data:
   Product     Month  Sales
6  Fridge  February  67000

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.