Reputation: 1293
I have a Dataframe with some sales data as below:
df = pd.DataFrame({'bill_id': ['1001','1002','1006']})
I have another set with a list of bill_id:
{'1002', '10006', '1009'}
I am trying to find bill_id
common in the Dataframe and the set and tried the below:
issues = list(l in set(df['bill_id']))
I get an error TypeError: 'bool' object is not iterable
, wondering if I am checking it incorrectly. Expecting the output to be a list.
Expected output of set as below:
{'1002'}
Upvotes: 1
Views: 840
Reputation: 552
You can also use isin
to get the rows in your dataframe that match your other list of ids.
In [1]: df = pd.DataFrame({'bill_id': ['1001','1002','1006']})
other_bill_ids = set({'1002', '10006', '1009'})
df[df['bill_id'].isin(other_bill_ids)]
Out[1]: bill_id
1 1002
If all you want is a list or a set, you can do
In [2]: df['bill_id'][df['bill_id'].isin(other_bill_ids)].tolist()
Out[2]: {'1002'}
or
In [3]: df['bill_id'][df['bill_id'].isin(other_bill_ids)].tolist()
Out[3]: ['1002']
Upvotes: 1
Reputation: 3739
1.Convert the dict into list
2.Fetch list of unique bill_id from dataframe
3.Use set and intersection method to get common elements
df = pd.DataFrame({'bill_id': ['1001','1002','1006']})
bill_id = list({'1002', '10006', '1009'})
bill_df = list(df['bill_id'].unique())
final_res = list(set(bill_df).intersection(bill_id))
print(final_res)
I hope it would solve your problem
Upvotes: 1
Reputation: 1207
Try something like that:
issues = set(df['bill_id']).intersection(other_bill_id_set)
Upvotes: 1
Reputation: 16886
df = pd.DataFrame({'bill_id': ['1001','1002','1006']})
bill_id = ('1002', '10006', '1009')
set(df.bill_id.values).intersection(bill_id)
Upvotes: 2