How to add values from dictionary as values in new column of df but associated with existing row by value of key in dictionary
import pandas as pd
data = {'caseno': ['123', '456', '789', '000'], 'defname': ['defendant1', 'defendant2', 'defendant3', 'defendant4']}
df = pd.DataFrame.from_dict(data)
def_dict = {'123': ['123address', '123address2', '123csz'], '456':['456address', '456address2', '456csz']}
caseno_lst = df['caseno'].tolist()
I thought this would work, but throws index error.
for i in caseno_lst:
for k, v in def_dict.items():
if k == i:
df['defadd'] = v
else:
pass
Expected output:
caseno defname defadd
0 123 defendant1 [123address, 123adress2, 123csz]
1 456 defendant2 [456address, 456address2, 456csz]
2 789 defendant3
3 000 defendant4
The issue is that my dictionary will not necessarily have a key that matches each case no in the df that I am trying to add columns and values to.