简体   繁体   English

pandas DataFrame 的列总和(但保留 pandas DataFrame 的结构)

[英]Column sums of a pandas DataFrame (but keep the structure of pandas DataFrame)

I have a small sample of my dataframe here:我在这里有一个 dataframe 的小样本:

df = pd.DataFrame([[1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 0, 0]], columns = ["CHAP1SEC1", "CHAP1SEC2", "CHAP1SEC3", "CHAP1SEC4", "CHAP1SEC5"], index = [0, 1, 2, 3])

Note: the variables are binary.注意:变量是二进制的。

打印的示例数据框

I'm trying to essentially merge these 4 rows into one row, keeping any non-zero entries in the columns.我试图基本上将这 4 行合并为一行,在列中保留任何非零条目。 Since the variables are binary, my go-to was just to take column sums.由于变量是二进制的,所以我的目标只是取列总和。

df.sum(axis = 1)

尝试输出打印

However, while this gives me the values I want, it is not returned in the same original dataframe structure.然而,虽然这给了我想要的值,但它并没有在相同的原始 dataframe 结构中返回。

Essentially, I would like to take the column sums of a dataframe, while keeping the structure of that dataframe.本质上,我想采用 dataframe 的列总和,同时保持 dataframe 的结构。 Ideally, my output would be as follows:理想情况下,我的 output 如下:

理想输出打印

I feel there must be a super simple solution that I am just not seeing and I couldn't find a similar question already posted on SO.我觉得一定有一个超级简单的解决方案,我只是没有看到,而且我找不到已经在 SO 上发布的类似问题。

Any help is appreciated任何帮助表示赞赏

here is one way to do it这是一种方法

df.sum(axis=0).to_frame().T

or或者

df.sum().to_frame().T

    CHAP1SEC1   CHAP1SEC2   CHAP1SEC3   CHAP1SEC4   CHAP1SEC5
0           1           0           1           1           1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM