Skip to content Skip to sidebar Skip to footer

Assign A Dictionary Value To A Dataframe Column Based On Dictionary Key

I'm looking to map the value in a dict to one column in a DataFrame where the key in the dict is equal to a second column in that DataFrame For example: If my dict is: dict = {'abc

Solution 1:

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.

Solution 2:

I would just do a simple map to get the answer.

If we have a dictionary as

d = {abc:1/2/2003, def:1/5/2017, ghi:4/10/2013}

And a dataframe as:

      Member    Group      Date

 0     xyz       A         np.Nan1     uvw       B         np.Nan2     abc       A         np.Nan3     def       B         np.Nan4     ghi       B         np.Nan

Then a simple map will solve the problem.

df["Date"] = df["Member"].map(d)

map() will lookup the dictionary for value in df['Member'], and for each value in Member, it will get the Value from dictionary d and assign it back to Date. If the value does not exist, it will assign NaN.

We don't need to do loop or apply.

Solution 3:

if Member is your index, you can assign a Series to the DataFrame:

df.set_index("Member", inplace=True)
df["Date"] = pd.Series(dict)

Pandas will match the index of the Series with the index of the DataFrame.

Solution 4:

for i in range(len(df)):
    ifdf['Member'][i] in d:
        df['Date'][i] = d[df['Member'][i]]

P.S. it's bad practise to name variables with reserved words (i.e. dict).

Solution 5:

Just create a new df then join them:

map_df = pd.DataFrame(list(zip(map_dict.items()))).set_index(0)
df.merge(map_df, how='left', left_on='Member', right_index=True)

Post a Comment for "Assign A Dictionary Value To A Dataframe Column Based On Dictionary Key"