计算当前行与上一行的差,我有一个简单的数据集和下面的代码:
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().
实现它的正确方法是什么?谢谢。
答案 0 :(得分:1)
按字典将numpy.sign
与map
一起使用:
sudo -H -u user bash -c 'firefox -marionette'
sudo -H -u user bash -c 'firefox -marionette'
sudo -H -u user bash -c 'firefox -marionette'
使用numpy.select
的另一种解决方案:
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
编辑:
s = df['Changes'].shift(1)
df['Changes'] = np.select([s < 0, s > 0, s == 0],
['Negative','Positive','Zero'],
default='')
答案 1 :(得分:0)
if
条件不适用于pandas.Series
。通过Changes
创建了df['Changes'] = (Rainfall.shift(-1) - Rainfall) / Rainfall
后,使用以下方法仅计算正数或负数-
df['Changes'] = np.where(df['Changes'] >0, 'positive', 'negative')
df['Changes'] = df['Changes'].shift(1)