0

I have a df:

  col1 col2
0  A   23
1  B   25
2  C   26
3  D   92

Now I need to filter this df on col1 to get the values for A and C. So I do the following:

filter1 = (df['col1'] == 'A') | (df['col1'] == 'C')
new_df = df[filter1]

This will give me the df with rows 0 and 2. However how can I make this configurable ? For example currently we are filtering on A or C maybe in the future I need to include D, or include B etc. How can I write this so that I make my filter configurable and just change the values in a list of something and the df filter gets updated?

Avi G
  • 67
  • 7

1 Answers1

1
yourList=["All the values you need to be filtered"]


df[df['col1'].isin(yourList)]
Suhas Mucherla
  • 1,383
  • 1
  • 5
  • 17