Pandas Pivot表:仅获取值计数,而不获取列

时间:2019-12-10 16:31:47

标签: pandas pivot-table

我正在尝试使用数据集多列的数据透视表。 当使用下面的代码制作数据透视表时,我得到了所有不需要的列。

我只想要计数,而不想要那里的任何其他列。我可以实现吗?

table1 = pd.pivot_table(dfCALCNoExcecption,index=['AD Platform','Agent Program'],columns=None,aggfunc='count')

上面的代码在excel输出中的输出如下所示(我没有粘贴整个内容,因为大约有50列): Sample Output with many more columns hidden

我想要获得的所需输出:

Desired Output with only counts of machine, without any other columns

2 个答案:

答案 0 :(得分:2)

您可以根据“ AD Plataform”和“ Agent Program”列对数据进行分组。之后,您可以对具有机器数量的列的所有值求和。这是我的代码:

df.groupby(['AD Plataform','Agent Program'])['AD主机名'] .sum()

答案 1 :(得分:0)

这还不完整,但是Groupby可以实现其中的一部分。我不确定如何将第三列重命名为“计数”

dfAgentTable3 =  dfCALCNoExcecption.groupby(['AD Platform', 'Agent Program'])['AD Hostname'].count().sort_index(ascending=True)

enter image description here