【问题标题】:Python Pandas iterrows() with previous values具有先前值的 Python Pandas iterrows()
【发布时间】:2014-10-17 20:24:48
【问题描述】:

我有一个熊猫数据框,格式如下:

            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]

【问题讨论】:

标签: python for-loop pandas dataframe


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    以下应该有效:

    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() 函数中,因此最终的索引输出是一个字符串,正如预期的那样.

    【讨论】:

      【解决方案3】:

      另一种方法可以是:

      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)
      

      【讨论】:

        猜你喜欢
        • 2019-04-14
        • 2022-11-02
        • 2014-07-22
        • 2017-09-07
        • 2022-09-23
        • 1970-01-01
        • 2016-06-10
        • 2014-06-02
        • 1970-01-01
        相关资源
        最近更新 更多