Skip to content
Advertisement

Substitute column values of a dataframe with the corresponding items in an array

I have a column in a dataframe which contains an array of numbers from 1 to 5 and I have an array containing five words. I would like to find the simplest, most compact and most elegant way in Python to “in place” replace the numbers in the column with the corresponding words. For example:

import pandas as pd

# This is the dataframe
df = pd.DataFrame({'code': ['A', 'B', 'C', 'D', 'E'], 'color': [4, 1, 2, 5, 1]})

#   code   color 
# 0    A       4 
# 1    B       1 
# 2    C       2 
# 3    D       5 
# 4    E       1

# This is the array
colors = ["blue", "yellow", "red", "white", "black"] 

# This is what I wish to obtain
#   code   color 
# 0    A   white 
# 1    B    blue 
# 2    C  yellow 
# 3    D   black 
# 4    E    blue

I am certain that the numbers in columns “color” are not NaN or outside range [1,5]. No check is necessary. Any suggestion?

Advertisement

Answer

This should do

df['color'] = df['color'].apply(lambda c: colors[c-1])
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement