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

Time:02-01

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?

CodePudding user response:

Create helper dictionary by enumerate and mapping values by Series.map, if no match is created missing values NaN:

df['color'] = df['color'].map(dict(enumerate(colors, 1)))
print (df)
  code   color
0    A   white
1    B    blue
2    C  yellow
3    D   black
4    E    blue

CodePudding user response:

This should do

df['color'] = df['color'].apply(lambda c: colors[c-1])
  • Related