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
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
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
Presuming you are using pandas, you could merge:
pd.merge(df1, df2, how='inner', on=['df1_col', 'df2_col'])