-1

Say I have a main_df that I'm working on:

ID      Name
 1      Bob
 2      Bill
 3      Bacon
 4      Bernie

and I have a separate_df with certain IDs that holds other data:

ID      Location
 1      California
 3      New York

How can I filter out the data in main_df so that only the rows with IDs found in separate_df remain?

ID      Name
 1      Bob
 3      Bacon

1 Answers1

1

Use Series.isin

main_df = pd.DataFrame([{'ID': 1, 'Name': 'Bob'}, {'ID': 2, 'Name': 'Bill'},
                        {'ID': 3, 'Name': 'Bacon'}, {'ID': 4, 'Name': 'Bernie'}])
loc_df = pd.DataFrame([{'ID': 1, 'Location': 'California'}, 
                       {'ID': 3, 'Location': 'New York'}, ])

main_df = main_df[main_df['ID'].isin(loc_df['ID'])]
azro
  • 53,056
  • 7
  • 34
  • 70