Python Pandas iterrows() 使用前一个值
我有一个 pandas 数据框,格式如下:
A B K S
2012-03-31 NaN NaN NaN 10
2012-04-30 62.74449 15.2 71.64 0
2012-05-31 2029.487 168.8 71.64 0
2012-06-30 170.7191 30.4 71.64 0
我想创建一个函数,用 df['S'][index-1] 的值来替换 df['S'] 的内容。
比如说:
for index,row in df.iterrows:
if index = 1:
pass
else:
df['S'] = min(df['A'] + df['S'][index-1]?? - df['B'], df['K'])
但是我不知道怎么获取 df['S'][index - 1] 的值。
3 个回答
1
另一种方法可以是:
for (index, row), ii in zip(df.iterrows(), range(len(df.index))):
# index: current row index
# row: current row
# df.iloc[ii-1]: prv row (of course make sure, prv row is present)
# df.iloc[ii+1]: next row (of course make sure, next row is present)
4
iterrows
的作用是一次处理一行数据,所以你无法访问之前的行。无论如何,你的函数会比较慢,而且还有一种更快的方法:
df['S_shifted'] = df.S.shift()
compared = pd.concat([df['A'] + df['S_shifted'] - df['B'], df['K']], axis=1)
df['S'] = compared.min(axis=1)
In [29]: df['S']
Out[29]:
2012-03-31 NaN
2012-04-30 57.54449
2012-05-31 71.64000
2012-06-30 71.64000
Name: S, dtype: float64
10
看起来你的初始答案已经很接近了。
下面的代码应该可以工作:
for index, row in df.iterrows():
if df.loc[index, 'S'] != 0:
df.loc[index, 'S'] = df.loc[str(int(index) - 1), 'S']
简单来说,除了第一个索引,也就是0以外,'S'这一列的值要改成它上面那一行的值。注意:这假设你的数据表是有序的,索引是连续的。
使用iterrows()
这个方法时,你不能直接通过行来修改值,所以你需要用df.loc()
来找到数据表中的单元格,然后再改变它的值。
还要注意的是,index
并不是一个整数,所以需要用int()
函数来减去1。所有这些操作都在str()
函数里面,这样最后输出的索引就是字符串,符合预期。