Creating new Pandas column based on conditions, but the values do not generate
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Switch On Looping
--
Chapters
00:00 Creating New Pandas Column Based On Conditions, But The Values Do Not Generate
01:09 Accepted Answer Score 1
01:30 Answer 2 Score 1
01:41 Thank you
--
Full question
https://stackoverflow.com/questions/5498...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas #dataframe
#avk47
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Switch On Looping
--
Chapters
00:00 Creating New Pandas Column Based On Conditions, But The Values Do Not Generate
01:09 Accepted Answer Score 1
01:30 Answer 2 Score 1
01:41 Thank you
--
Full question
https://stackoverflow.com/questions/5498...
--
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)))