简体   繁体   中英

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM