El Cid
El Cid

Reputation: 301

Pandas Add column based on dict value of another column

Given a dataframe df, it contains the following columns : col1, col2, col3

Col1 contains string values (possible duplicate values) as shown :

Val1
Val2
Val3
Val1
Val1
Val1

Additionally, I have a dictionary storing mapping of Col1 -> new values that I need to add to the dataframe 'df'. Example :

{
    "Val1" : "new_val1",
    "Val2" : "new_val2",
    "Val3" : "new_val3",
}

Now, based on the dictionary mapping, I want to add 'col_new' to the 'df'. Sample final dataframe (showing only relevant columns) :

col1  col_new
Val1  new_val1
Val2  new_val2
Val3  new_val3
Val1  new_val1
Val1  new_val1
Val1  new_val1

I tried df.map(), but that seems to only work when the key column in dataframe is unique.

Suggestions ?

Upvotes: 3

Views: 4494

Answers (2)

Nour-Allah Hussein
Nour-Allah Hussein

Reputation: 1459

In addition to the answer of @sophods you can also

df['col_new'] = df['col1'].apply(lambda x:your_dict[x])

Added based on comment

import pandas as pd

df=pd.DataFrame({'col1':['Val1','Val2','Val3','Val1','Val1','Val1']})
your_dict={
    "Val1" : "new_val1",
    "Val2" : "new_val2",
    "Val3" : "new_val3",
    }

df['col_new'] = df['col1'].apply(lambda x:your_dict[x])
print(df)

output

   col1   col_new
0  Val1  new_val1
1  Val2  new_val2
2  Val3  new_val3
3  Val1  new_val1
4  Val1  new_val1
5  Val1  new_val1

Upvotes: 1

sophocles
sophocles

Reputation: 13841

This should do the trick, use map:

your_dict={
    "Val1" : "new_val1",
    "Val2" : "new_val2",
    "Val3" : "new_val3",
}

df['col_new'] = df['col1'].map(your_dict)

which prints your desired output:

   col1   col_new
0  Val1  new_val1
1  Val2  new_val2
2  Val3  new_val3
3  Val1  new_val1
4  Val1  new_val1
5  Val1  new_val1

Upvotes: 4

Related Questions