python - 定义趋势 Pandas / python

标签 python pandas trend

我有数据集:

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

M/A

如何确定最后 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/

相关文章:

R计算几年来每天的趋势

python - 在 Python 中以十进制形式显示 float ?

python - 在mongoengine中查询列表;包含与在

python __getattr__ 帮助

python - Pandas:将数据框划分为一些部分

python - Pandas 为一列附加多列

python - 非常大的矩阵上的 Pandas 交叉表?

c++ - 如何检查数字序列是否在C++中具有增加/减少趋势

algorithm - 数据是否存在线性趋势?

python - 使用 Groupby 时调用具有多个参数的函数