How to use List of Values to Select Rows from pandas Dataframe

Spread the love

Problem –

You have a pandas dataframe

import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/bprasad26/lwd/master/data/clothing_store_sales.csv')
df.head()

And you want to select all the rows of data where the Method of Payment is Discover and MasterCard. You want to select rows from a pandas dataframe based on multiple values.

Solution –

If you want to filter based on a single value, you can filter like this.

df[df['Method of Payment']=='Discover']

But to select rows based on multiple list of values you can use the isin method.

df[df['Method of Payment'].isin(['Discover','MasterCard'])]

Rating: 1 out of 5.

Leave a Reply