I have a dataframe and a dictionary
df
col1 col2
a a1
b b1
c c1
d d1
dict
company = {
'abcd': [a1],
'efgh': [b1],
'rewr': [c1],
'fsdf': [d1]
}
I need to create new column from dictionary key if the value matches the column in df so that my output df is:
col1 col2 new_col
a a1 abcd
b b1 efgh
c c1 rewr
d d1 fsdf
my current loop for that is below but is there a more pythonic way of doing this:
def find_value(v):
found = False
for k in dic.keys():
if v in dic[k]:
return k
if not found: return None
df['new_col'] = dataset['col2'].apply(find_value)