Reputation: 11
I have a dataframe with a column having company names: such as
ID NAME dict_key
1 APPLE NaN
2 GOOGLE Nan
3 IBM NaN
4 HP NaN
5 SAMSUNG NaN
...
And i have a dict key and multiple values for corresponding keys, such as
{1000:['APPLE INC', 'APPLE COMPUTER INC', 'AOPLE INC', 'APPLE'], 1001:['GOOGLE INC', 'GOOGLE','GOOLELL INC']}.....
I want to write a loop to see if dataframe second column company names in the dictionary, if they are matched, like APPLE in first row and first column going to match APPLE in dict with key 1000, then I want to assign the key to dict_key column. Thanks a lot.
Upvotes: 1
Views: 2297
Reputation: 435
So I think that you'd want to apply a custom function that would run over all the key value pairs, checking if the value in df['NAME'] is in the value.
def match_company_to_key(company_name):
for key, value in your_dict.items():
if company_name in value
return key
df['dict_key'] = df['NAME'].apply(match_company_to_key)
Upvotes: 0
Reputation: 5183
Try iterating over the dictionary and calling series.update
with each list of names:
for key, names in my_dict.items():
df.dict_key.update(df.name.isin(names) * key) # True * 1000 = 1000
Upvotes: 1