我不需要按数字Age
进行分组,而是需要按在数据框架上替换的特定年龄段进行分组
import pandas as pd
# intialise data of lists.
data = {'Name':['Tom', 'nick', 'krish', 'jack','Ann','James'],
'Age':[20, 21, 45, 58,34,60]}
# Create DataFrame
df = pd.DataFrame(data)
这是我尝试过的:
if df['Age'] < 20:
df['Age']= df['Age'].replace([<20],'<20')
if df['Age'] >= 20 & >40:
df['Age']= df['Age'].replace([>=20&<40],'>=20&<40')
if df['Age'] >=40:
df['Age']= df['Age'].replace([>=40],'>=40')
答案 0 :(得分:1)
使用np.select(setofconditions, matchingchoices
)
import numpy as np
c1=df['Age'] < 20
c2=df['Age'].between(20,40)
c3=df['Age'] >=40
cond=[c1,c2,c3]
choice=['<20','>=20&<40','>=40']
df['agerange']=np.select(cond,choice)
Name Age agerange
0 Tom 20 >=20&<40
1 nick 21 >=20&<40
2 krish 45 >=40
3 jack 58 >=40
4 Ann 34 >=20&<40
5 James 60 >=40