我有一个DataFrame,看起来像这样:
import Data.List
keepl k n [] = []
keepl k 1 (x:xs) = x : (keepl k k xs)
keepl k n (x:xs) = keepl k (n-1) xs
main = do
print $ keepl 3 3 [1,2,3,4,5,6,7,8,9]
我想添加一个新列,在Excel中计算如下:= IF((B2-B1)> 0; B2-B1; 0),所以它应该如下所示:
Date Price
0 Jan 04, 1999 1.1827
1 Jan 05, 1999 1.1764
2 Jan 06, 1999 1.1618
3 Jan 07, 1999 1.1709
4 Jan 08, 1999 1.1575
5 Jan 11, 1999 1.1502
6 Jan 12, 1999 1.1572
7 Jan 13, 1999 1.1673
8 Jan 14, 1999 1.1698
9 Jan 15, 1999 1.1557
实际上,我有一个解决方案,但我对此并不满意:
Date Price up
0 Jan 04, 1999 1.1827 0.0000
1 Jan 05, 1999 1.1764 0.0000
2 Jan 06, 1999 1.1618 0.0000
3 Jan 07, 1999 1.1709 0.0091
4 Jan 08, 1999 1.1575 0.0000
5 Jan 11, 1999 1.1502 0.0000
6 Jan 12, 1999 1.1572 0.0070
7 Jan 13, 1999 1.1673 0.0101
8 Jan 14, 1999 1.1698 0.0025
9 Jan 15, 1999 1.1557 0.0000
还有更优雅的解决方案吗?谢谢!
答案 0 :(得分:2)
dif = df['Price'].diff()
df['up'] = dif.where(dif > 0, 0)
#alternative
#df['up'] = np.where(dif > 0, dif, 0)
print (df)
Date Price up
0 Jan 04, 1999 1.1827 0.0000
1 Jan 05, 1999 1.1764 0.0000
2 Jan 06, 1999 1.1618 0.0000
3 Jan 07, 1999 1.1709 0.0091
4 Jan 08, 1999 1.1575 0.0000
5 Jan 11, 1999 1.1502 0.0000
6 Jan 12, 1999 1.1572 0.0070
7 Jan 13, 1999 1.1673 0.0101
8 Jan 14, 1999 1.1698 0.0025
9 Jan 15, 1999 1.1557 0.0000