Python Pandas Assign a Dictionary Value to a Dataframe Column Based on Dictionary Key
You can use df.apply
to solve your problem, where d
is your dictionary.
df["Date"] = df["Member"].apply(lambda x: d.get(x))
What this code does is takes every value in the Member
column and will look for that value in your dictionary. If the value is found in the dictionary than the corresponding dictionary value will populate the column. If the value is not in the dictionary then None
will be returned.
Also, make sure your dictionary contains valid data types. In your dictionary the keys (abc, def, ghi) should be represented as strings and your dates should be represented as either strings or date objects.