Reputation: 181
Hi I have a variable 'age' in my data and i want to bin it in python and save it into a new variable. something like this"
data['new_age']= if data['age'] <= 16 then 'young'
if data['age'] > 16 & data['age'] <= 35 then 'adult'
else 'old' end
how can i do this with a pandas data frame?
Upvotes: 0
Views: 1167
Reputation: 4929
Besides the @trigonom classic pythonic solution (explicitly answering your question), other options include:
Using numpy.select
:
df['new_age'] = np.select([df.age.lt(16), (df.age.gt(16)) & (df.age.le(35))], ['young','adult'], 'old')
Or pd.cut
(as pointed out by @Mike Tomaino):
df['new_age'] = pd.cut(df.age, [0, 16, 35, np.inf], labels=['young', 'adult', 'old'])
Upvotes: 1
Reputation: 528
def new_age(r):
if r['age']<=16:
return 'young'
if r['age'] <=35
return 'adult'
return 'old'
df['new_age'] = df.apply(lambda x: new_age(x),axis = 1)
completely disagree with your age dividing, 35 is a young adult
Upvotes: 2