Python Pandas iterrows() 与以前的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25473153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:23:26  来源:igfitidea点击:

Python Pandas iterrows() with previous values

pythonfor-looppandasdataframe

提问by Stavros Anastasiadis

I have a pandas Dataframe in the form:

我有一个 Pandas Dataframe 的形式:

            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

I trying to create a function that replace df['S'] using df['S'][index-1] value.

我试图创建一个使用 df['S'][index-1] 值替换 df['S'] 的函数。

for example:

例如:

for index,row in df.iterrows:
     if index = 1: 
         pass
     else:
         df['S'] = min(df['A'] + df['S'][index-1]?? - df['B'], df['K'])

but i dont know how to get df['S'][index - 1]

但我不知道如何获得 df['S'][index - 1]

回答by TomAugspurger

The point of iterrowsis to operate one row at a time, so you won't be able to access prior rows. Your function will be slow anyways, and there's a much faster way:

重点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

回答by Fab Dot

It looks like your initial answer is pretty close.

看起来您的初始答案非常接近。

The following should work:

以下应该工作:

for index, row in df.iterrows():
    if df.loc[index, 'S'] != 0:
        df.loc[index, 'S'] = df.loc[str(int(index) - 1), 'S']

Essentially, for all but the first index, i.e. 0, change the value in the 'S' column to the value in the row before it. Note: This assumes a dataframe with a sequential, ordered index.

本质上,对于除第一个索引(即 0)以外的所有索引,将“S”列中的值更改为其前一行中的值。注意:这假设数据帧具有连续、有序的索引。

The iterrows()method doesn't let you modify the values by calling the row on its own, hence you need to use df.loc()to identify the cell in the dataframe and then change it's value.

iterrows()方法不允许您通过自己调用行来修改值,因此您需要使用df.loc()来标识数据框中的单元格,然后更改它的值。

Also worth noting that indexis not an integer, hence the the use of the int()function to subtract 1. This is all within the str()function so that the final index output is a string, as expected.

还值得注意的是,index它不是整数,因此使用int()函数来减 1。这一切都在str()函数内,因此最终的索引输出是一个字符串,正如预期的那样。