我有数据集:
print (df['price'])
0 0.435
1 -2.325
2 -3.866
...
58 -35.876
59 -37.746
Name: price, dtype: float64
移动平均线:
m_a = df['price'].rolling(window=5).mean()
m_a.plot()
print(m_a)
0 NaN
1 NaN
2 NaN
3 NaN
4 -2.8976
5 -4.9628
...
58 -36.2204
59 -36.4632
如何确定最后 n 行的趋势 - 平/涨/跌? 在文本或 int def 结果中,例如:
trend = gettrend(df,5)
print(trend)
>>UP
最佳答案
您可以将类似的东西与 np.where 一起使用并根据需要扩展逻辑:
df['Trend'] = np.where(df['m_a'] < df['m_a'].shift(),'DOWN',
np.where(df['m_a'] > df['m_a'].shift(),'UP','FLAT'))
price m_a Trend
0 1 2 FLAT
1 2 2 FLAT
2 3 4 UP
3 4 5 UP
4 5 6 UP
5 6 7 UP
6 7 -1 DOWN
7 8 2 UP
8 6 7 UP
9 7 -6 DOWN
10 8 -7 DOWN
关于python - 定义趋势 Pandas / python ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41599501/