在 Python Dataframe 中对行求和

2022-11-05Python开发问题
430

本文介绍了在 Python Dataframe 中对行求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我刚开始学习 Python,如果这个问题已经在其他地方得到回答,请原谅我.我想创建一个名为Sum"的新列,它只是之前添加的列.

I just started learning Python so forgive me if this question has already been answered somewhere else. I want to create a new column called "Sum", which will simply be the previous columns added up.

Risk_Parity.tail()

    VCIT  VCLT  PCY     RWR     IJR     XLU     EWL
Date                            
2017-01-31  21.704155   11.733716   9.588649    8.278629    5.061788    7.010918    7.951747
2017-02-28  19.839319   10.748690   9.582891    7.548530    5.066478    7.453951    7.950232
2017-03-31  19.986782   10.754507   9.593623    7.370828    5.024079    7.402774    7.654366
2017-04-30  18.897307   11.102380   10.021139   9.666693    5.901137    7.398604    11.284331
2017-05-31  63.962659   23.670240   46.018698   9.917160    15.234977   12.344524   20.405587

表格列有点偏,但我只需要 (21.70 + 11.73...+7.95)我只能创建列 Risk_Parity['sum'] = ,但后来我迷路了.

The table columns are a little off but all I need is (21.70 + 11.73...+7.95) I can only get as far as creating the column Risk_Parity['sum'] = , but then I'm lost.

我宁愿不必这样做 Risk_Parity['sum] = Risk_Parity['VCIT'] + Risk_Parity['VCLT']...

创建总和列后,我想将每一列除以总和列,并将其制成一个新的数据框,其中不包括总和列.

After creating the sum column, I want to divide each column by the sum column and make that into a new dataframe, which wouldn't include the sum column.

如果有人能提供帮助,我将不胜感激.请尽量降低你的答案,哈哈.

If anyone could help, I'd greatly appreciate it. Please try to dumb your answers down as much as possible lol.

谢谢!

汤姆

推荐答案

使用 sum 和参数 axis=1 指定行的总和

Use sum with the parameter axis=1 to specify summation over rows

Risk_Parity['Sum'] = Risk_Parity.sum(1)

创建 Risk_Parity 的新副本而不向原始列写入新列

To create a new copy of Risk_Parity without writing a new column to the original

Risk_Parity.assign(Sum= Risk_Parity.sum(1))

<小时>

还要注意,我将列命名为 Sum 而不是 sum.我这样做是为了避免与我用来创建列的名为 sum 的相同方法发生冲突.


Notice also, that I named the column Sum and not sum. I did this to avoid colliding with the very same method named sum I used to create the column.

只包含数字列...但是,sum 无论如何都知道要跳过非数字列.

To only include numeric columns... however, sum knows to skip non-numeric columns anyway.

RiskParity.assign(Sum=RiskParity.select_dtypes(['number']).sum(1))
# same as
# RiskParity.assign(Sum=RiskParity.sum(1))

             VCIT   VCLT    PCY   RWR    IJR    XLU    EWL     Sum
Date                                                              
2017-01-31  21.70  11.73   9.59  8.28   5.06   7.01   7.95   71.33
2017-02-28  19.84  10.75   9.58  7.55   5.07   7.45   7.95   68.19
2017-03-31  19.99  10.75   9.59  7.37   5.02   7.40   7.65   67.79
2017-04-30  18.90  11.10  10.02  9.67   5.90   7.40  11.28   74.27
2017-05-31  63.96  23.67  46.02  9.92  15.23  12.34  20.41  191.55

这篇关于在 Python Dataframe 中对行求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

pandas 有从特定日期开始的按月分组的方式吗?
Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)...
2024-08-22 Python开发问题
10

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10