I have a dataframe "result" and want to create a new column called "type". The value in "type" will be the item value of a dict if the column "Particulars" in the dataframe contains value of the key.
dict_classify={'key1': 'content1',
'key2':'content2'
}
result['type']=[dict_classify[key] if key.lower() in i.lower() else np.nan
for key in dict_classify.keys()
for i in result['Particulars']]
It returns the error "Length of values (5200) does not match the length of index (1040)". Any idea what I did wrong?
The following is what I want to achieve in a normal for loop. Can I make it into one line?
lst_type=[]
for i in result['Particulars']:
for key in dict_classify:
temp=np.nan
if key.lower() in i.lower():
temp=dict_classify[key]
break
lst_type.append(temp)
result['type']=lst_type