熊猫 - 累积计算组

发布于 2025-02-09 12:16:30 字数 4832 浏览 1 评论 0原文

我正在尝试使用Jupyter笔记本中使用Pandas进行累积操作进行计算。

但是我卡住了。

我有以下数据框架:

日期类型存款提款
2020/01/19A10000
2020/01/23A5000
2020/02/05A0200
2020/02/22A1000200
2020/01/19B859.80 0
2020/02/04B514.3350
2020/02/25b1463.140

我需要应用此公式,按类型进行分组:

Movement = IF(ISNULL([previous_date]); [Current_Deposit] - [withdrawal];([previous_movement] * (1 +([current_date]-[previous_date]) * 0,0001)) + [Current_Deposit] - [withdrawal])

示例:

日期类型的存款戒断戒烟公式
2020/01/1000a100001000if(isnull(isnull)(iSnull) ); [1000] - [0];([0] *(1 +([2020/01/19] - [null]) * 0,0001)) + [1000] - [1000] - [0]
) /23a50001500.40if(isnull([2020/01/19])); [500] - [0];([1000] *(1 +([2020/01/23]) - [2020/01/19 ]) * 0,0001)) + [500] - [0])
2020/02/05a02001302.35如果(isnull([2020/01/23])); [0] - [0] - [200];([1500.40) ] *(1 +([2020/02/05] - [2020/01/23]) * 0,0001)) + [0] - [200]
2020/02/22A10002002104,56) isnull([2020/02/05]); [1000] - [200];([1302.35] *(1 +([2020/02/22] + [1000] - [200])
2020/01/19B859.80859.80if(isnull([Null]); [859.8] - [0] - [0];([0] *(1 +([2020/01/19) ] - [null]) * 0,0001)) + [859.8] - [0])
2020/02/04B514.33501325.51if(isnull([2020/01/19]); [514.33] - [50];([[859.80] *(1 +([2020/02/04] - [2020/01/19]) * 0,0001)) + [514.33] - [50] - [50])
2020/) 02/25B1463.1402791.43如果(ISNULL([[2020/02/04])); [1463.14] - [0] - [0];([1325.51] *([1 +([2020/02/25]) - 04]) * 0,0001)) + [1463.14] - [0])

我无法累积要放入公式中的值。

我需要按类型对计算进行分组,并累积在当前行中使用的值。 我是Python的新手,我无法实施它。

I'm trying to do a calculation with cumulative operations using pandas in jupyter notebook.

But I'm stuck.

I have the following DataFrame:

DateTypedepositwithdrawal
2020/01/19A10000
2020/01/23A5000
2020/02/05A0200
2020/02/22A1000200
2020/01/19B859.80
2020/02/04B514.3350
2020/02/25B1463.140

I need to apply this formula, grouping by type:

Movement = IF(ISNULL([previous_date]); [Current_Deposit] - [withdrawal];([previous_movement] * (1 +([current_date]-[previous_date]) * 0,0001)) + [Current_Deposit] - [withdrawal])

Example:

DateTypedepositwithdrawalMovementFormula
2020/01/19A100001000IF(ISNULL([NULL]); [1000] - [0];([0] * (1 +([2020/01/19]-[NULL]) * 0,0001)) + [1000] - [0])
2020/01/23A50001500.40IF(ISNULL([2020/01/19]); [500] - [0];([1000] * (1 +([2020/01/23]-[2020/01/19]) * 0,0001)) + [500] - [0])
2020/02/05A02001302.35IF(ISNULL([2020/01/23]); [0] - [200];([1500.40] * (1 +([2020/02/05]-[2020/01/23]) * 0,0001)) + [0] - [200])
2020/02/22A10002002104,56IF(ISNULL([2020/02/05]); [1000] - [200];([1302.35] * (1 +([2020/02/22]-[2020/02/05]) * 0,0001)) + [1000] - [200])
2020/01/19B859.80859.80IF(ISNULL([NULL]); [859.8] - [0];([0] * (1 +([2020/01/19]-[NULL]) * 0,0001)) + [859.8] - [0])
2020/02/04B514.33501325.51IF(ISNULL([2020/01/19]); [514.33] - [50];([859.80] * (1 +([2020/02/04]-[2020/01/19]) * 0,0001)) + [514.33] - [50])
2020/02/25B1463.1402791.43IF(ISNULL([2020/02/04]); [1463.14] - [0];([1325.51] * (1 +([2020/02/25]-[2020/02/04]) * 0,0001)) + [1463.14] - [0])

I'm not able to accumulate the values to put in the formula.

I need to group the calculations by type, and accumulate the values to use in the current line.
I'm new to python and I'm not able to implement it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魂ガ小子 2025-02-16 12:16:31

我认为您的数据帧按类型和日期进行排序

# silencing chained assignment warning
pd.options.mode.chained_assignment = None

df['Movement'] = 0

df_new = df
for row in df.iterrows():
    x = row[1]
    y = df_new.shift().loc[x.name]
    if x['Type'] == y['Type']:
        # computing the given days compound interest should calculate using power
        compound_interest = pow(1.0001, pd.Timedelta(pd.to_datetime(x['Date']) - pd.to_datetime(y['Date'])).days)
        # if you wish to use your formula uncomment the following line
        # compound_interest = 1 + 0.0001 * pd.Timedelta(pd.to_datetime(x['Date']) - pd.to_datetime(y['Date'])).days
        df_new['Movement'].loc[x.name] = compound_interest * y['Movement'] + x['deposit'] - x['withdrawal']
    else:
        df_new['Movement'].loc[x.name] = x['deposit']

print(df_new)

输出

      Date Type  deposit  withdrawal    Movement
2020/01/19    A  1000.00           0 1000.000000
2020/01/23    A   500.00           0 1500.400060
2020/02/05    A     0.00         200 1302.351751
2020/02/22    A  1000.00         200 2104.567521
2020/01/19    B   859.80           0  859.800000
2020/02/04    B   514.33          50 1325.506712
2020/02/25    B  1463.14           0 2791.433062

I assume that your dataframe is sorted by Type and Date

# silencing chained assignment warning
pd.options.mode.chained_assignment = None

df['Movement'] = 0

df_new = df
for row in df.iterrows():
    x = row[1]
    y = df_new.shift().loc[x.name]
    if x['Type'] == y['Type']:
        # computing the given days compound interest should calculate using power
        compound_interest = pow(1.0001, pd.Timedelta(pd.to_datetime(x['Date']) - pd.to_datetime(y['Date'])).days)
        # if you wish to use your formula uncomment the following line
        # compound_interest = 1 + 0.0001 * pd.Timedelta(pd.to_datetime(x['Date']) - pd.to_datetime(y['Date'])).days
        df_new['Movement'].loc[x.name] = compound_interest * y['Movement'] + x['deposit'] - x['withdrawal']
    else:
        df_new['Movement'].loc[x.name] = x['deposit']

print(df_new)

OUTPUT

      Date Type  deposit  withdrawal    Movement
2020/01/19    A  1000.00           0 1000.000000
2020/01/23    A   500.00           0 1500.400060
2020/02/05    A     0.00         200 1302.351751
2020/02/22    A  1000.00         200 2104.567521
2020/01/19    B   859.80           0  859.800000
2020/02/04    B   514.33          50 1325.506712
2020/02/25    B  1463.14           0 2791.433062
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文