2021年1月20日星期三

filter multiple separate rows in a DataFrame that meet the condition in another DataFrame with pandas? [duplicate]

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?

https://stackoverflow.com/questions/65820596/filter-multiple-separate-rows-in-a-dataframe-that-meet-the-condition-in-another January 21, 2021 at 11:02AM

没有评论:

发表评论