0

This is my DataFrame

df = pd.DataFrame({'uid': [109200005, 108200056, 109200060, 108200085, 108200022],
    'grades': [69.233627, 70.130900, 83.357011, 88.206387, 74.342212]})

This is my condition list which comes from another DataFrame

condition_list = [109200005, 108200085]

I use this code to filter records that meet the condition

idx_list = []
for i in condition_list:
    idx_list.append(df[df['uid']==i].index.values[0])

and get what I need

>>> df.iloc[idx_list]
uid grades
0   109200005   69.233627
3   108200085   88.206387

Job is done. I'd just like to know is there a simpler way to do the job?

PutBere
  • 131
  • 1
  • 12

1 Answers1

1

Yes, use isin:

df[df['uid'].isin(condition_list)]
Tom
  • 8,310
  • 2
  • 16
  • 36