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

0

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.