Windstorm1981
Windstorm1981

Reputation: 2680

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':'1/2/2003', 'def':'1/5/2017', 'ghi':'4/10/2013'}

and my DataFrame is:

      Member    Group      Date
 0     xyz       A         np.Nan
 1     uvw       B         np.Nan
 2     abc       A         np.Nan
 3     def       B         np.Nan
 4     ghi       B         np.Nan

I want to get the following:

      Member    Group      Date
 0     xyz       A         np.Nan
 1     uvw       B         np.Nan
 2     abc       A         1/2/2003
 3     def       B         1/5/2017
 4     ghi       B         4/10/2013

Note: The dict doesn't have all the values under "Member" in the df. I don't want those values to be converted to np.Nan if I map. So I think I have to do a fillna(df['Member']) to keep them?


Unlike Remap values in pandas column with a dict, preserve NaNs which maps the values in the dict to replace a column containing the a value equivalent to the key in the dict. This is about adding the dict value to ANOTHER column in a DataFrame based on the key value.

Upvotes: 28

Views: 46941

Answers (5)

Joe Ferndz
Joe Ferndz

Reputation: 8508

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.Nan
 1     uvw       B         np.Nan
 2     abc       A         np.Nan
 3     def       B         np.Nan
 4     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.

Upvotes: 10

Gregor Sturm
Gregor Sturm

Reputation: 2910

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.

Upvotes: 2

PMende
PMende

Reputation: 5460

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)

Upvotes: -1

vielkind
vielkind

Reputation: 2980

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.

Upvotes: 40

bigEvilBanana
bigEvilBanana

Reputation: 398

for i in range(len(df)):
    if df['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).

Upvotes: -1

Related Questions