2

我有一个这样的数据框,

df
col1    col2
  A       1
  B       3
  C       2
  D       5
  E       6
  F       8
  G       10

我想添加 col2 特定值的上一个和下一个 n 值并将其存储到一个新列中,

所以,如果 n=2,那么数据框应该是这样的,

 col1    col2    col3
  A       1       6  (only below 2 values are there no upper values, so adding 3 numbers)
  B       3      11 (adding one prev, current and next two)
  C       2      17(adding all 4 values)
  D       5      24(same as above)
  E       6      31(same as above)
  F       8      29(adding two prev and next one as only one is present)
  G       10     24(adding with only prev two values)

当找不到前一个或下一个 2 值时,添加任何可用的值。我可以使用 for 循环来做到这一点,但是执行时间会很长,寻找一些 pandas 快捷方式可以最有效地做到这一点。

4

1 回答 1

5

您可以使用该rolling方法。

import pandas as pd
df = pd.read_json('{"col1":{"0":"A","1":"B","2":"C","3":"D","4":"E","5":"F","6":"G"},"col2":{"0":1,"1":3,"2":2,"3":5,"4":6,"5":8,"6":10}}')

df['col3'] = df['col2'].rolling(5, center=True, min_periods=0).sum()
col1    col2    col3
0   A   1   6.0
1   B   3   11.0
2   C   2   17.0
3   D   5   24.0
4   E   6   31.0
5   F   8   29.0
6   G   10  24.0
于 2020-10-10T19:57:47.077 回答