答案 0 :(得分:0)
从它的外观来看,您可以使用 pandas apply 并传递一个适当的 lambda 函数,该函数将前一行的 cash_remaining
值作为参数,例如使用 .loc 或 .iloc。
答案 1 :(得分:0)
该方法取决于每个月是否有一些额外的逻辑:
from datetime import datetime as dt
import numpy as np
import pandas as pd
df = pd.DataFrame.from_dict({
'date': [dt(2008, 4, 30), dt(2008, 5, 3), dt(2008, 6, 30), dt(2008, 7, 31), dt(2008, 8, 29)],
'NYSEARCA:PYZ': [36.37, 38.52, 35.82, 35.80, 34.86],
'amount_available': [2100] + [np.nan]*4,
'amount_spent': [1984.2] + [np.nan]*4,
'cash_remaining': [115.8] + [np.nan]*4
})
method_1 = df.copy()
# if there is "row-by-row" dependencies
for i, row in method_1.iterrows():
if not np.isnan(row['cash_remaining']):
cash_rem = row['cash_remaining']
else:
# dummy additional time-series dependent logic
cash_rem_old = cash_rem
spent = round(np.random.rand(1).item() * 20, 2) # whatever
cash_rem = cash_rem_old - spent
row[['amount_available', 'amount_spent', 'cash_remaining']] = [cash_rem_old, spent, cash_rem]
method_1.iloc[i] = row
# if values just need to be copied over
method_2 = df.copy()
target = method_2['cash_remaining'][0]
method_2.loc[1:, ['amount_available', 'amount_spent', 'cash_remaining']] = [target, 0.0, target]
print(method_1)
# date NYSEARCA:PYZ amount_available amount_spent cash_remaining
# 0 2008-04-30 36.37 2100.00 1984.20 115.80
# 1 2008-05-03 38.52 115.80 14.81 100.99
# 2 2008-06-30 35.82 100.99 0.96 100.03
# 3 2008-07-31 35.80 100.03 17.65 82.38
# 4 2008-08-29 34.86 82.38 7.24 75.14
print(method_2)
# date NYSEARCA:PYZ amount_available amount_spent cash_remaining
# 0 2008-04-30 36.37 2100.0 1984.2 115.8
# 1 2008-05-03 38.52 115.8 0.0 115.8
# 2 2008-06-30 35.82 115.8 0.0 115.8
# 3 2008-07-31 35.80 115.8 0.0 115.8
# 4 2008-08-29 34.86 115.8 0.0 115.8