如何基于层次结构计算列值

时间:2019-02-24 18:21:07

标签: python pandas numpy

让我们说,我们在层次结构上同意以下顺序。

婴儿->儿童->青少年->成人

我有这个数据集

           Name         Stage  Highest_Stage_Reached
0          Adam         Child  
1         Barry         Child
2           Ben         Adult
3          Adam      Teenager
4         Barry         Adult
5           Ben         Baby

我将如何设置数据集来填充这样的Highest_Stage_Reached字段?

           Name         Stage  Highest_Stage_Reached
0          Adam         Child  Teenager
1         Barry         Child  Adult
2           Ben         Adult  Adult
3          Adam      Teenager  Teenager
4         Barry         Adult  Adult
5           Ben         Baby   Adult

3 个答案:

答案 0 :(得分:3)

您可以使用:

d={'Baby':0,'Child':1,'Teenager':2,'Adult':3}
df['rank']=df.Stage.map(d)
df['Highest_Stage_Reached']=df.groupby('Name')['rank'].transform('max').\
                                         map({v: k for k, v in d.items()})
print(df.drop('rank',1))

    Name     Stage Highest_Stage_Reached
0   Adam     Child              Teenager
1  Barry     Child                 Adult
2    Ben     Adult                 Adult
3   Adam  Teenager              Teenager
4  Barry     Adult                 Adult
5    Ben      Baby                 Adult

答案 1 :(得分:1)

使用列表的索引将层次结构放入列表中。

l = ['Baby', 'Child', 'Teenager', 'Adult']
df = pd.DataFrame({'Name': ['Adam', 'Barry', 'Ben', 'Adam', 'Barry', 'Ben'], 'Stage': ['Child', 'Child', 'Adult', 'Teenager', 'Adult', 'Baby']})

cond = [df['Stage'] == 'Baby',df['Stage'] == 'Child',df['Stage'] == 'Teenager',df['Stage'] == 'Adult']
df['Highest_Stage_Reached'] = np.select(cond, [0,1,2,3])

    Name     Stage  Highest_Stage_Reached
0   Adam     Child                      1
1  Barry     Child                      1
2    Ben     Adult                      3
3   Adam  Teenager                      2
4  Barry     Adult                      3
5    Ben      Baby                      0

df['Highest_Stage_Reached'] = (df.groupby('Name')['Highest_Stage_Reached'].transform(max))

    Name     Stage  Highest_Stage_Reached
0   Adam     Child                      2
1  Barry     Child                      3
2    Ben     Adult                      3
3   Adam  Teenager                      2
4  Barry     Adult                      3
5    Ben      Baby                      3


df['Highest_Stage_Reached'] = df['Highest_Stage_Reached'].apply(lambda x: l[x])
print(df)

输出:

    Name     Stage Highest_Stage_Reached
0   Adam     Child              Teenager
1  Barry     Child                 Adult
2    Ben     Adult                 Adult
3   Adam  Teenager              Teenager
4  Barry     Adult                 Adult
5    Ben      Baby                 Adult

答案 2 :(得分:1)

使用order参数将列转换为分类列。现在,您可以对其进行排序。这还将支持Stage中可变数量的参数。

df['Stage'] = pd.Categorical(df['Stage'], ordered=True, categories=['Baby', 'Child','Teenager','Adult'])

df['Highest_Stage_Reached'] = df.groupby('Name').Stage.transform('max')

    Name    Stage       Highest_Stage_Reached
0   Adam    Child       Teenager
1   Barry   Child       Adult
2   Ben     Adult       Adult
3   Adam    Teenager    Teenager
4   Barry   Adult       Adult
5   Ben     Baby        Adult