我有一个包含2列的a
和b
的DataFrame,我想基于以下三个条件填充第三列c
:
a.diff() > 0
则c = b.shift() + b
a.diff() < 0
然后c = b.shift() - b
a.diff() == 0
然后c = b.shift()
什么是Pythonic,单行方式?
示例:
a b c
0 2 10 Nan
1 3 16 26
2 1 12 4
3 1 18 12
4 3 11 29
5 1 13 -2
答案 0 :(得分:2)
使用numpy.select
并缓存shift
ed和diff
ed Series
,以获得更好的效果和可读性:
diff = df.a.diff()
shifted = df.b.shift()
df['c'] = np.select([diff > 0, diff < 0], [shifted + df.b, shifted - df.b], default=shifted)
print (df)
a b c
0 2 10 NaN
1 3 16 26.0
2 1 12 4.0
3 1 18 12.0
4 3 11 29.0
5 1 13 -2.0