1

I am trying to update the values of column 'Age' using the apply() method. I want to update the ages into 2 specific values. This is the function.

def new_age(a):
    if a<25:
       return 'pro'
    else:
       return 'notpro'

When I pass the apply function df['Age'].apply(new_age) it works fine but when i try to update the values of the "Age" column using df['Age']=df['Age'].apply(new_age) it returns NaN. Can I get some help? Edit: I tried the map() function. It did nothing better.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
Suraj
  • 135
  • 1
  • 9

2 Answers2

1

Try this:

import numpy as np

df['Age'] = np.where(df['Age'] < 25, 'pro', 'notpro')

This will be much, much faster than calling a function on each element.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
1
# imports, creataion of example dataframe
import pandas as pd
df=pd.DataFrame([20,30,22,28],columns=['age'])

If want to stick with apply(), this works for me:

df.loc[:, 'age'] = df.age.apply(new_age)

or, if you don't like .loc:

df.age[:] = df.age.apply(new_age)

Can you a lambda function as well:

df.age[:] = df.age.apply(lambda age: 'pro' if age<25 else 'notpro')
zabop
  • 6,750
  • 3
  • 39
  • 84