Python 根据表中的行值创建新列

Python 根据表中的行值创建新列,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个熊猫数据框,看起来像这样: id name total cubierto no_cubierto escuela_id nivel_id 0 1 direccion 1 1 0 420000707 1 1 2 frente_a_alunos 4 4 0 420000707 1

我有一个熊猫数据框,看起来像这样:

     id             name  total  cubierto  no_cubierto  escuela_id  nivel_id 
0   1        direccion      1         1            0   420000707         1   
1   2  frente_a_alunos      4         4            0   420000707         1   
2   3            apoyo      2         2            0   420000707         1   
3   4        direccion      2         2            0   840477414         2   
4   5  frente_a_alunos      8         8            0   840477414         2   
5   6            apoyo      4         3            1   840477414         2   
6   7        direccion      7         7            0   918751515         3   
7   8            apoyo     37        37            0   918751515         3   
8   9        direccion      1         1            0   993683216         1   
9  10  frente_a_alunos      7         7            0   993683216         1   
escuela_id      nivel_id   apoyo_cubierto   apoyo_total   direccion_total  
0   420000707         1              2           2                1   
1   840477414         2              3           4                2   
2   918751515         3             37          37                7   
3   993683216         1             ..          ..                1   


   direccion_cubierto    frente_a_alunos_total    frente_a_alunos_cubierto  
0                   1                     4                        4  
1                   2                     8                        8  
2                   7                    ..                       ..  
3                   1                     7                        7  
“名称”列有3个唯一值:

 - direccion
 - frente a alunos
 - apoyo
我需要得到一个新的数据帧,按“escuela_id”和“nivel_id”分组,其中包含以下列:

 - direccion_total
 - direccion_cubierto
 - frente_a_alunos_total
 - frente_a_alunos_cubierto
 - apoyo_total
 - apoyo_cubierto
 - escuela_id
 - nivel_id
分别从列
“total”
“cubierto”
中获取值。 我不需要列
“no\u cubierto”
。 有没有可能用熊猫的功能呢?我被它绊住了,找不到任何解决办法

示例的输出应如下所示:

     id             name  total  cubierto  no_cubierto  escuela_id  nivel_id 
0   1        direccion      1         1            0   420000707         1   
1   2  frente_a_alunos      4         4            0   420000707         1   
2   3            apoyo      2         2            0   420000707         1   
3   4        direccion      2         2            0   840477414         2   
4   5  frente_a_alunos      8         8            0   840477414         2   
5   6            apoyo      4         3            1   840477414         2   
6   7        direccion      7         7            0   918751515         3   
7   8            apoyo     37        37            0   918751515         3   
8   9        direccion      1         1            0   993683216         1   
9  10  frente_a_alunos      7         7            0   993683216         1   
escuela_id      nivel_id   apoyo_cubierto   apoyo_total   direccion_total  
0   420000707         1              2           2                1   
1   840477414         2              3           4                2   
2   918751515         3             37          37                7   
3   993683216         1             ..          ..                1   


   direccion_cubierto    frente_a_alunos_total    frente_a_alunos_cubierto  
0                   1                     4                        4  
1                   2                     8                        8  
2                   7                    ..                       ..  
3                   1                     7                        7  

您需要在此处使用透视表:

df = df.pivot_table(index=['escuela_id', 'nivel_id'], columns='name', values=['total', 'cubierto']).reset_index()
df.columns = ['_'.join(col).strip() for col in df.columns.values]
print(df)
输出:

   escuela_id_  nivel_id_  cubierto_apoyo  cubierto_direccion  cubierto_frente_a_alunos  total_apoyo  total_direccion  total_frente_a_alunos
0    420000707          1             2.0                 1.0                       4.0          2.0              1.0                    4.0
1    840477414          2             3.0                 2.0                       8.0          4.0              2.0                    8.0
2    918751515          3            37.0                 7.0                       NaN         37.0              7.0                    NaN
3    993683216          1             NaN                 1.0                       7.0          NaN              1.0                    7.0

Pandas有一个函数用于此。向我们展示您的代码,我们可以通过一些反馈帮助您,guidanceI刚刚发布了一个示例,展示了输出应该是什么样子