我正在将一个函数转换为循环集合的pandas,并根据条件和运行总计更新每个值。该函数看起来像这样
def calculate_value():
cumulative_amount = 0
for row in rows:
if row['amount'] < 0:
return 0
amount = 0
if row['kind'] == 'A':
amount = row['amount'] * row['input_amount']
elif row['kind'] == 'B':
amount = row['input_amount'] - cumulative_amount
elif row['kind'] == 'C':
amount = row['amount']
cumulative_amount += amount
row['result'] = amount
if row['kind'] == 'B':
break
return rows
基本上,遍历所有行,并添加result
值。但是此result
可能取决于累计运行总计。此外,如果我们达到某个值(row['kind'] == 'B'
),我们应该打破并停止处理新行。
将其转换为pandas时,似乎我应该使用apply
。到目前为止,我有以下代码,其中几乎有效,但当我尝试使用cumulative_amount
获取shift(-1)
时,它总是以nan
的形式返回。< / p>
在熊猫中做到这一点的最佳方式是什么?
def calculate_value(row: Series):
if row['amount'] < 0 or row.shift(-1)['kind'] == 'B':
row['cumulative_amount'] = 0
row['result'] = 0
return row
amount = 0
if np.isnan(row.shift(-1)['cumulative_amount']):
cumulative_amount = 0
else:
cumulative_amount = row.shift(-1)['cumulative_amount']
if row['kind'] == 'A':
amount = row['amount'] * row['input_amount']
elif row['kind'] == 'B':
amount = row['input_amount'] - cumulative_amount
elif row['kind'] == 'C':
amount = row['amount']
row['cumulative_amount'] = amount + cumulative_amount
row['result'] = amount
return row
df['cumulative_amount'] = 0
new_df = df.apply(lambda x: calculate_value(x), axis=1)
输入和所需输出的示例是
df = pd.DataFrame({
'kind': {1: 'C', 2: 'E', 3: 'A', 4: 'A', 5: 'B', 6: 'C'},
'amount': {1: -800, 2: 100, 3: 0.5, 4: 0.5, 5: 0, 6: 200},
'input_amount': {1: 800, 2: 800, 3: 800, 4: 800, 5: 800, 6: 800}
})
amount input_amount kind cumulative_amount result
1 -800.0 800 C 0.0 0.0
2 100.0 800 E 0.0 0.0
3 0.5 800 A 400.0 400.0
4 0.5 800 A 800.0 400.0
5 0.0 800 B 800.0 0.0
6 200.0 800 C 800.0 0.0
答案 0 :(得分:1)
如果我理解正确,只有result
种类'B'
取决于其他行。所以你可以先做其他事情开始:
df['result'] = 0.
a = (df.kind == 'A') & (df.amount >= 0)
c = (df.kind == 'C') & (df.amount >= 0)
df.loc[a, 'result'] = df.loc[a, 'amount'] * df.loc[a, 'input_amount']
df.loc[c, 'result'] = df.loc[c, 'amount']
做cumsum:
df['cumulative_amount'] = df.result.cumsum()
更正'cumulative_amount'
的值(对于所有类型'B'
的出现次数):
df.loc[(df.kind == 'B'), 'result'] = df.loc[(df.kind == 'B'), 'input_amount'].values - df.loc[(df.kind.shift(-1) == 'B'), 'cumulative_amount'].values
在第一次出现'result'
后更正'cumulative_amount'
和'B'
的值:
df.loc[(df.kind == 'B').cumsum().shift() > 0, 'result'] = 0
# (df.kind == 'B').cumsum().shift() is a running count of the number of B's encountered prior to the row index,
# so you want to 'stop' once this number is no longer zero
# You could of course do this more simply by figuring out which position in the index has the first B,
# then using .ix or .iloc, but it's actually longer to type out.
df['cumulative_amount'] = df.result.cumsum() # Once more, because we've changed the value of results below B.