Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pandas-根据dataframe的两个独立列中的值构造列_Python_Pandas_Dataframe_Pivot - Fatal编程技术网

Python pandas-根据dataframe的两个独立列中的值构造列

Python pandas-根据dataframe的两个独立列中的值构造列,python,pandas,dataframe,pivot,Python,Pandas,Dataframe,Pivot,我有一个与此类似的pandas数据框(我已经制作了一个示例,因为我无法共享数据) 输出为: regiment company thisValue total 0 Nighthawks 1st 1 3 1 Nighthawks 2nd 2 3 2 Dragoons 1st 3 5 3 Dragoons 2nd 2 5 4

我有一个与此类似的pandas数据框(我已经制作了一个示例,因为我无法共享数据)

输出为:

    regiment    company thisValue   total
0   Nighthawks  1st         1         3
1   Nighthawks  2nd         2         3
2   Dragoons    1st         3         5
3   Dragoons    2nd         2         5
4   Scouts      2nd         7         7
我想统计一个团每个值的数值。也就是说,我需要生成的数据帧如下所示:

regiment    1stCompanyValue 2nd_Company_Value   total
Nighthawks         1               2              3
Dragoons           3               2              5
Scouts             0               7              7
我试着根据公司的价值观来分组,但不知道如何进行。如何在熊猫身上做到这一点

我们可以利用,即


正是我想要的。谢谢你能给我一个小的解释吗?是的,我会这么做:)这些都是非常简单的函数,如果我解释它们,我担心我会把你弄糊涂,所以你可以选择文档。熊猫医生在这方面真的很好。
regiment    1stCompanyValue 2nd_Company_Value   total
Nighthawks         1               2              3
Dragoons           3               2              5
Scouts             0               7              7
one  = df.pivot(columns='company',values='thisValue',index='regiment').add_suffix('_company_value').fillna(0)
two = df.groupby('regiment')['total'].first()

ndf = pd.concat([one,two],1)

              1st_company_value  2nd_company_value  total
regiment                                               
Dragoons                  3.0                2.0      5
Nighthawks                1.0                2.0      3
Scouts                    0.0                7.0      7