The Python Oracle

Creating new Pandas column based on conditions, but the values do not generate

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Quirky Dreamscape Looping

--

Chapters
00:00 Question
01:52 Accepted answer (Score 1)
02:14 Answer 2 (Score 1)
02:38 Thank you

--

Full question
https://stackoverflow.com/questions/5498...

Question links:
[this very popular one]: https://stackoverflow.com/questions/2688...

Accepted answer links:
[numpy.select()]: https://docs.scipy.org/doc/numpy/referen...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas #dataframe

#avk47



ACCEPTED ANSWER

Score 1


You can use numpy.select():

df['col']=np.select([df.x.eq(1),df.x.eq(2),df.x.eq(3)],['small','medium','large'],\
                                                                  'something')

you can replace 'something' with the value which should appear when the conditions are not met.

print(df)

   x    size
0  1   small
1  2  medium
2  3   large



ANSWER 2

Score 1


You can try more simple version:

import pandas as pd
import numpy as np

df = pd.read_csv('blah.csv')

def size(x):
    if x == 1:
        return 'Small'
    if x == 2:
        return 'Medium'
    if x == 3:
        return 'Large'
    return -99 

# maybe your row type is"string"
df['size'] = df['rQ7'].apply (lambda x: size(int(x)))