熊猫,重命名groupby调用中的重复列

时间:2019-02-06 15:23:27

标签: python pandas dataframe

我正在使用熊猫计算数据框的元素。 我的数据看起来像

A    B    C    
001  003  1
001  003  1
001  003  0 
...   

我正在使用代码:

exampleDataFrame = exampleDataFrame.groupby(['A','B','C'])['C'].size()

其中C只能是1或0。(我需要查看在C中每个A和B出现多少个1和0)。

这给了我如下结果:

A    B    C    C
001  003  1    2    (There were 2 '1's in C for columns A and B)
001  003  0    1
... 

但是现在我需要将第4列('C')重命名为更唯一的名称,例如'D'。最好的方法是什么?

谢谢!

1 个答案:

答案 0 :(得分:2)

尝试 if (pidVal == 0) { printf("Child Process (should be 0): %d\n", pidVal); do { val = getInput(); printf("You typed: %d\n", val); //Write to parent close(fd[0]); write(fd[1], &val, sizeof(val)); } while (val != -1); close(fd2[1]); read(fd2[0], &final, sizeof(final)); //If sum sent from parent, print and terminate if (final != -9999999) { printf("%d\n", final); exit(0); } } 分组后的系列,然后reset_index以获取数据帧:

rename

输出:

df.groupby(['A','B','C'])['C'].size().rename('D').reset_index()
相关问题