ByRequest
ByRequest

Reputation: 311

Using isin to return values from two specific columns from two Pandas dataframes

I have two dataframes with identifiers for physical facilities. I then have a list of facilities. I would like to return only the locations used in both dataframes from my master list. I am attempting to get the following to work, and it appears I have something in the syntax incorrect, or am not using isin properly.

print(filtered_locations[filtered_locations['id'].isin(filtered_departments.buildingid,filtered_stores.facilityid)])

I have tried a few variations of this - while there are many examples in the documents that specifically show how to scan two lists using dictionaries etc. I am having trouble sourcing the quickest/most direct way to return only values that are in the two specified columns in the other dataframes.

Upvotes: 0

Views: 520

Answers (1)

null
null

Reputation: 2137

Refer to the documentation here for pandas.Series.isin() method.

It only accepts one parameter which is

values : set or list-like

what you have given is tuple of two lists, so you need to merge your two lists in a single list-like or set, set is probably much better as it will not contain duplicated values.

Code

ids = set(filtered_departments.buildingid.tolist() +\
filtered_stores.facilityid.tolist())

my_locations = filtered_locations[filtered_locations['id'].isin(ids)]

Upvotes: 2

Related Questions