Home > Blockchain >  Optimise data filtering in dataframe
Optimise data filtering in dataframe

Time:09-23

Is there a better/faster way to filter the dataframe df2 by column user_option whose can be those from the list the entire u_choices.

I've written the code below at present.

u_choices = ['a1', 'b1', 'b2', 'c3' 'd2', 'e4']

df1 = df2[(df2['user_option'] == 'a1') | (df2['user_option'] == 'b1')| (df2['user_option'] == 'b2')| (df2['user_option'] == 'c3')| (df2['user_option'] == 'a2d2)| (df2['user_option'] == 'e4')]

When dealing with dataframe with hundreds of thousands of rows across multiple areas, the slow down in performance is pretty significant & hence I'm looking to optimize the operation.

Any help would be grea please.

CodePudding user response:

Yes, you can use .isin(...)

df1 = df2[df2['user_option'].isin(u_choices)]
  • Related