Selecting multiple columns based on conditional values

Selecting multiple columns based on conditional values in a Pandas DataFrame is a powerful technique for filtering and extracting specific subsets of data. By using boolean conditions, you can define criteria that columns must meet to be included in the selection, providing greater control and flexibility in data manipulation. This approach allows you to efficiently retrieve relevant information from your dataset, facilitating complex data analysis and decision-making processes.

Create a DataFrame with data

import pandas as pd import numpy as np df = pd.DataFrame() df['Name'] = ['John', 'Doe', 'Bill','Jim','Harry','Ben'] df['TotalMarks'] = [82, 38, 63,22,55,40] df['Grade'] = ['A', 'E', 'B','E','C','D'] df['Promoted'] = [True, False,True,False,True,True]
Name TotalMarks Grade Promoted 0 John 82 A True 1 Doe 38 E False 2 Bill 63 B True 3 Jim 22 E False 4 Harry 55 C True 5 Ben 40 D True

Select all column with conditional values

example-1
df[df['Grade'] == 'E']
Name TotalMarks Grade Promoted 1 Doe 38 E False 3 Jim 22 E False
example-2
df[df['TotalMarks'] > 50]
Name TotalMarks Grade Promoted 0 John 82 A True 2 Bill 63 B True 4 Harry 55 C True

Select two columns with conditional values

df[['Name','TotalMarks']] [df['Promoted'] == True]
Name TotalMarks 0 John 82 2 Bill 63 4 Harry 55 5 Ben 40

Select all column with multiple conditional values

df[(df['TotalMarks'] > 50) & (df['TotalMarks'] < 80) ]
Name TotalMarks Grade Promoted 2 Bill 63 B True 4 Harry 55 C True

Select two column with multiple conditional values

example-1
df[['Name','TotalMarks']][(df["Promoted"] == True) & (df["Grade"] == 'A')]
Name TotalMarks 0 John 82
example-2
df[['Name','TotalMarks']] [(df['TotalMarks'] > 50) & (df['TotalMarks'] < 80) ]
Name TotalMarks 2 Bill 63 4 Harry 55

How to Select multiple columns based on conditional values pandas dataframe

Using isin()

The isin() method in Pandas is a valuable tool for performing membership tests within a DataFrame. It allows you to check whether each element in a DataFrame belongs to a specified list of values or not. This function is particularly useful for filtering data based on specific criteria and identifying rows that match certain conditions.

df[df.Grade.isin(['E'])]
Name TotalMarks Grade Promoted 1 Doe 38 E False 3 Jim 22 E False

isin() with multiple values

filter = df['Grade'].isin(['A', 'B']) df[filter]
Name TotalMarks Grade Promoted 0 John 82 A True 2 Bill 63 B True

Also, you can retrieve data of selected columns using isin and loc

df.loc[df['Grade'].isin(['A', 'B']),['Name','TotalMarks', 'Grade'] ]
Name TotalMarks Grade 0 John 82 A 2 Bill 63 B

Conclusion

The isin() method in Pandas enables the selection of multiple columns from a DataFrame based on specific conditional values. It allows you to efficiently filter and retrieve rows that match the conditions specified in the provided list of values, making it a powerful tool for data manipulation and analysis.