发现 pandas 的增长趋势 [英] Finding increasing trend in Pandas

查看:54
本文介绍了发现 pandas 的增长趋势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一组(时间序列)数据,如何以增/减,不稳定,不变等方式解释数据.

Given a set of (time-series) data, how to interpret the data in such a way that it is increasing/decreasing, not steady, unchanged, etc.

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

推荐答案

您可以使用numpy.polyfit,也可以提供阶数作为拟合多项式的度.

you can use numpy.polyfit, you can provide order as Degree of the fitting polynomial.

引用: numpy.polyfit文档

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,则趋势是恒定的,否则就在减小

so now if the value of slope is +ve the trend is increasing, if it is 0 trend is constant, else decreasing

在您给定的数据中,斜率是0.0804761904762.因此,趋势正在增加

In your given data slope is 0.0804761904762. So, the trend is increasing

这篇关于发现 pandas 的增长趋势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆