发现大Pandas的增长趋势

2024-09-28 21:31:09 发布

您现在位置:Python中文网/ 问答频道 /正文

给定一组(时间序列)数据,如何以这样的方式解释该数据,即它是递增/递减的、不稳定的、不变的等

Year  Revenue
1993     0.85
1994     0.99
1995     1.01
1996     1.12
1997     1.25
1998     1.36
1999     1.28
2000     1.44

Tags: 数据方式时间序列yearrevenue
2条回答

如果数据帧按'Year'排序

df.sort_values('Year', inplace=True)

然后可以观察pd.Series属性
^{}
^{}
^{}

您可以使用numpy.polyfit,您可以提供顺序作为拟合多项式的次数。

参考:numpy.polyfit documentation

import numpy as np
import pandas as pd

def trendline(data, order=1):
    coeffs = np.polyfit(data.index.values, list(data), order)
    slope = coeffs[-2]
    return float(slope)

#Sample Dataframe
revenue = [0.85, 0.99, 1.01, 1.12, 1.25, 1.36, 1.28, 1.44]
year = [1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000]
df = pd.DataFrame({'year': year, 'revenue': revenue})


slope = trendline(df['revenue'])
print slope

所以现在如果斜率的值是+ve,趋势是增加的,如果是0,趋势是不变的,否则是减少的

在给定的数据中,斜率为0.0804761904762。所以,趋势是

相关问题 更多 >