1

I have a df that looks like this (except more columns and rows):

p_id
1
2
3

How do I create logic that is scalable to map certain values to certain numbers in the p_id column?

example df should look like:

p_id: 
a
b
c

in other words how do I make logic that says for every 1 in column p_id, change to a

RustyShackleford
  • 3,462
  • 9
  • 40
  • 81

1 Answers1

1

you could use Series.map & pass in a dictionary.

df = pd.DataFrame({'p_id': [1,2,3]})
df.p_id.map({1: 'a', 2: 'b', 3: 'c'})
#output:
0    a
1    b
2    c
Name: p_id, dtype: object

However, if your mapping an integer to an letter, you could use the chr function

# 97 is the ascii code for `a`
(df.p_id+96).map(chr)
#outputs:
0    a
1    b
2    c
Name: p_id, dtype: object
Haleemur Ali
  • 26,718
  • 5
  • 61
  • 85