<p>我很惊讶没有一个本土的熊猫解决方案,因为转移和滚动没有完成。我已经设计了一种使用标准pandas语法的方法,但是我不确定它是否比您的循环执行得更好。。。我的目的只是为了一致性(而不是速度)。</p>
<pre><code>import pandas as pd
df = pd.DataFrame({'a':[0,1,2], 'b':[0,10,20]})
new_col = 'c'
def apply_func_decorator(func):
prev_row = {}
def wrapper(curr_row, **kwargs):
val = func(curr_row, prev_row)
prev_row.update(curr_row)
prev_row[new_col] = val
return val
return wrapper
@apply_func_decorator
def running_total(curr_row, prev_row):
return curr_row['a'] + curr_row['b'] + prev_row.get('c', 0)
df[new_col] = df.apply(running_total, axis=1)
print(df)
# Output will be:
# a b c
# 0 0 0 0
# 1 1 10 11
# 2 2 20 33
</code></pre>
<p>免责声明:我使用了pandas 0.16,但只做了一点点修改,这也适用于最新版本。</p>
<p>其他人也有类似的问题,我也在这些问题上发布了这个解决方案:</p>
<ul>
<li><a href="https://stackoverflow.com/questions/16698415/reference-previous-row-when-iterating-through-dataframe/38155257#38155257">Reference previous row when iterating through dataframe</a></li>
<li><a href="https://stackoverflow.com/questions/23333786/reference-values-in-the-previous-row-with-map-or-apply/38155070#38155070">Reference values in the previous row with map or apply</a></li>
</ul>