计算从当前行到上一行的差异,我有一个简单的数据集和下面的代码:
import pandas as pd
data = {'Month' : [1,2,3,4,5,6,7,8,9,10,11,12],
'Rainfall': [112,118,132,129,121,135,148,148,136,119,104,118]}
df = pd.DataFrame(data)
Rainfall = df["Rainfall"]
df['Changes'] = Rainfall.shift(-1) - Rainfall
df['Changes'] = df['Changes'].shift(1)
它显示了更改(如左图所示)。但是我只在乎变化是正,负还是零(如图片的右侧)
我试图添加一个IF条件,但它给了我错误:
if df['Changes'] > 0:
df['Changes'] = df['Changes'].shift(1)
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
什么是实现它的正确方法?谢谢。
最佳答案
通过字典将numpy.sign
与map
一起使用:
d = {1:'Positive', -1:'Negative',0:'Zero'}
df['Changes'] = np.sign(df['Changes'].shift(1)).map(d).fillna('')
print (df)
Month Rainfall Changes
0 1 112
1 2 118 Positive
2 3 132 Positive
3 4 129 Negative
4 5 121 Negative
5 6 135 Positive
6 7 148 Positive
7 8 148 Zero
8 9 136 Negative
9 10 119 Negative
10 11 104 Negative
11 12 118 Positive
numpy.select
的另一种解决方案:s = df['Changes'].shift(1)
df['Changes'] = np.select([s < 0, s > 0, s == 0],
['Negative','Positive','Zero'],
default='')
编辑:
df['Changes'] = df['Changes'].shift(1)
bins = np.arange(-100, 100, step=5)
labels = ['{}-{}'.format(i, j) for i, j in zip(bins[:-1], bins[1:])]
df['Changes'] = pd.cut(df['Changes'], bins=bins, labels=labels)
print (df)
Month Rainfall Changes
0 1 112 NaN
1 2 118 0-5
2 3 132 0-5
3 4 129 -5-0
4 5 121 -5-0
5 6 135 0-5
6 7 148 0-5
7 8 148 -5-0
8 9 136 -5-0
9 10 119 -5-0
10 11 104 -5-0
11 12 118 0-5
关于python - 总结当前行与上一行的区别,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54760821/