简体   繁体   English

如何在 pandas 中相互添加/减去行值?

[英]How to add/subtract row values from each other in pandas?

I have a DataFrame that looks like:我有一个 DataFrame,看起来像:

    credit  debit
0   0.0     89.40
1   0.0     4.33
2   0.0     12.00
3   500.0   0.00
4   0.0     5.40

If I know the current balance is 300, how do I make a balance column that subtracts the debit from each row and adds the credit from each row?如果我知道当前余额是 300,我如何制作一个余额列,从每一行中减去借方并从每一行中添加贷方?

I have tried using df.sub but I am having no luck...我试过使用df.sub但我没有运气......

The Balance column should look like:余额列应如下所示:

balance
300.00
389.40
393.33
405.33
-94.27

here is the code for the sample DataFrame这是示例 DataFrame 的代码

df = pd.DataFrame({'credit': {0: 0.0, 1: 0.0, 2: 0.0, 3: 500.0, 4: 0.0},'debit': {0: 89.4, 1: 4.33, 2: 12.0, 3: 0.0, 4: 5.4}})

Like this:像这样:

In [511]: df['balance'] = df.debit.cumsum() + 300 - df.credit.cumsum()

In [512]: df
Out[512]: 
   credit  debit  balance
0     0.0  89.40   389.40
1     0.0   4.33   393.73
2     0.0  12.00   405.73
3   500.0   0.00   -94.27
4     0.0   5.40   -88.87

OR:要么:

In [513]: df['balance'] = df.debit.cumsum().sub(df.credit.cumsum()).add(300)

I am thinking that you are looking for a "running balance" of the dataframe. If yes, continue to read else discard.我认为您正在寻找 dataframe 的“运行余额”。如果是,请继续阅读,否则请丢弃。

import pandas as pd
from itertools import accumulate
import operator
import numpy as np

DF = pd.DataFrame({'credit': {0: 0.0, 1: 0.0, 2: 0.0, 3: 500.0, 4: 0.0},
 'debit': {0: 89.4, 1: 4.33, 2: 12.0, 3: 0.0, 4: 5.4}})

Initial_Balance = 300
Running_Balance = np.array(list(accumulate(DF.debit-DF.credit, func=operator.add)))+Initial_Balance
DF["Running_Balance"] = Running_Balance
print(DF)

This gives the following output.这给出以下 output。

   credit  debit  Running_Balance
0     0.0  89.40           389.40
1     0.0   4.33           393.73
2     0.0  12.00           405.73
3   500.0   0.00           -94.27
4     0.0   5.40           -88.87

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

相关问题 Pandas - 如何根据其他列减去行值? - Pandas - How to subtract row values based on other columns? 如何从数字中减去熊猫DataFrame的每一行? - How to subtract each row of a pandas DataFrame from a number? 如何在熊猫中将两列列表相减? - How to subtract two columns of lists from each other in pandas? 如何有效地从熊猫数据框中减去每一行? - How to efficiently subtract each row from pandas dataframe? pandas数据帧中的日期时间不会相互减去 - Datetime in pandas dataframe will not subtract from each other Pandas,如何从系列中的所有值中减去第一行值? - Pandas, How to subtract first row value from all values in a Series? 熊猫:从行中的每个元素中减去行均值 - Pandas: Subtract row mean from each element in row Pandas:如何获取一列中每个项目的最后每日值并从每行中的值中减去它 - Pandas: How to get last daily value for each item in one column and subtract it from the value in each row 如何从 pandas dataframe 中的当前行中减去前一行以创建一个新列,以每个名称重新启动进程? - How to subtract previous row from current row in a pandas dataframe to create a new column restarting the process with each name? 如何从pyspark的每隔一行中减去spark数据帧中的每一行? - How to Subtract Each row in spark data frames from every other row in pyspark?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM