0

I'm transitioning from R to Python and trying to subset a dataframe with a field in another dataframe. What would be the Python Equivalent for this R code:

final_solution <- subset(df1, item %in% df2$item)

Thanks

2
  • Are you using pandas? Commented Dec 19, 2015 at 18:09
  • yes i'm using pandas Commented Dec 19, 2015 at 19:40

2 Answers 2

2

We can try

df1[(df1.item).isin(df2.item)]

Using a reproducible example (with pandas)

import pandas as pd
df1 = pd.DataFrame({'item' :  [1, 2, 3, 4],
                           'fruit' : ['mango', 'apple', 'banana', 'mango']})

df2 = pd.DataFrame({'item' : [1, 2]})

print(df1[(df1.item).isin(df2.item)])

gives the output

#    fruit  item
#0  mango     1
#1  apple     2
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I was looking for.
2

Presuming you are using pandas, you could merge:

pd.merge(df1, df2, how='inner', on=['df1_col', 'df2_col'])

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.