0
df = pd.DataFrame({'a': [1,2,3,4,5], 'b': [5,6,7,3,1]})

Here's what I would like to do, but of course generates an error:

dfnew = df[df['a'] in [2, 4]]

I want dfnew to contain all the rows for which the values in column 'a' are in the list [2, 4]. I know I can do that in this way (for this toy example):

dfnew = df[(df['a']==2) | (df['a']==4)]

However, the actual dataset has thousands of columns, and the list has over a thousand entries, so hopefully there's a simpler way to accomplish this.

Rafael_Espericueta
  • 435
  • 1
  • 6
  • 14

0 Answers0