Amit Amit - 12 days ago 5
Python Question

Calculate average age based on condition on age column in python

I have a R code that i am struggling to translate in python. Please help

df$age[(df$age < 18)] <- mean(df$age[(df$age >= 18) & (df$age <=30)],na.rm=TRUE)

Answer

EDIT: I added None in data to get NaN in DataFrame


Probably

df.age[(df.age < 18)] = df.age[(df.age >= 18) & (df.age <= 30)].mean()

Working example

import pandas as pd

data = {'age': [6, 4, 18, 21, 30, 45, None]}

df = pd.DataFrame(data)

print(df)
print('---')

df.age[(df.age < 18)] = df.age[(df.age >= 18) & (df.age <= 30)].mean()

print(df)
print('---')

Result

    age
0   6.0
1   4.0
2  18.0
3  21.0
4  30.0
5  45.0
6   NaN
---
    age
0  23.0
1  23.0
2  18.0
3  21.0
4  30.0
5  45.0
6   NaN
---
Comments