Python Subset dataframe with another dataframe
Question: 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


Answer: 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
    

