2

I have a large dataframe as follows:

master_df

             result   item
0            5        id13
1            6        id23432
2            3        id2832
3            4        id9823
......
84376253     7        id9632

And another smaller dataframe as follows:

df = pd.DataFrame({'item' : ['id9632', 'id13', 'id2832', 'id2342']})

How can I extract the relevant elements from master_df.result to match with df.item so I can achieve the following:

df = df.assign(result=list_of_results_in_order)
0

2 Answers 2

1

You can do merge also:

df = df.merge(master_df, on='item', how='left)
Sign up to request clarification or add additional context in comments.

Comments

1

I think need isin with boolean indexing:

#for Series
s = master_df.loc[master_df['item'].isin(df['item']),'result']
print (s)
0           5
2           3
84376253    7
Name: result, dtype: int64

#for list
L = master_df.loc[master_df['item'].isin(df['item']),'result'].tolist()
print (L)
[5, 3, 7]

#for DataFrame
df1 = master_df[master_df['item'].isin(df['item'])]
print (df1)
          result    item
0              5    id13
2              3  id2832
84376253       7  id9632

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.