Pandas:添加交叉表总计

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26932682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:40:20  来源:igfitidea点击:

Pandas: add crosstab totals

pythonpandas

提问by meto

How can I add to my crosstab an additional row and an additional column for the totals?

如何在交叉表中为总计添加额外的行和额外的列?

df = pd.DataFrame({"A": np.random.randint(0,2,100), "B" : np.random.randint(0,2,100)})
ct = pd.crosstab(new.A, new.B)
ct

enter image description here

在此处输入图片说明

I thought I would add the new column (obtained by summing over the rows) by

我以为我会添加新列(通过对行求和获得)

ct["Total"] = ct.0 + ct.1

but this does not work.

但这不起作用。

采纳答案by joris

This is because 'attribute-like' column access does not work with integer column names. Using the standard indexing:

这是因为“类似属性”的列访问不适用于整数列名。使用标准索引:

In [122]: ct["Total"] = ct[0] + ct[1]

In [123]: ct
Out[123]:
B   0   1  Total
A
0  26  24     50
1  30  20     50

See the warnings at the end of this section in the docs: http://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access

请参阅文档中本节末尾的警告:http: //pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access

When you want to work with the rows, you can use .loc:

当您想处理行时,可以使用.loc

In [126]: ct.loc["Total"] = ct.loc[0] + ct.loc[1]

In this case ct.loc["Total"]is equivalent to ct.loc["Total", :]

在这种情况下ct.loc["Total"]相当于ct.loc["Total", :]

回答by Ida

In fact pandas.crosstabalready provides an option margins, which does exactly what you want.

事实上pandas.crosstab已经提供了一个选项margins,它完全符合你的要求。

> df = pd.DataFrame({"A": np.random.randint(0,2,100), "B" : np.random.randint(0,2,100)})
> pd.crosstab(df.A, df.B, margins=True)
B     0   1  All
A               
0    26  21   47
1    25  28   53
All  51  49  100

Basically, by setting margins=True, the resulting frequency table will add an "All" column and an "All" row that compute the subtotals.

基本上,通过设置margins=True,生成的频率表将添加一个“全部”列和一个“全部”行来计算小计。

回答by Shiny

You should use the margins=True for this along with crosstab. That should do the job!

为此,您应该将 margins=True 与交叉表一起使用。那应该做的工作!