在 Pandas 中寻找增长趋势

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42920537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:14:47  来源:igfitidea点击:

Finding increasing trend in Pandas

pythonpandas

提问by Aristotle Tan Yi Sing

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

回答by Ashish

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

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

Refer:numpy.polyfit documentation

参考: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

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

所以现在如果斜率的值是 +ve 趋势正在增加,如果它是 0 趋势是恒定的,否则减少

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

在您给定的数据斜率为 0.0804761904762。所以趋势是越来越大

回答by piRSquared

if you sort the dataframe by 'Year'

如果您对数据框进行排序 'Year'

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

You can then observe the pd.Seriesattributes
df.Revenue.is_monotonic
df.Revenue.is_monotonic_decreasing
df.Revenue.is_monotonic_increasing

然后你可以观察pd.Series属性
df.Revenue.is_monotonic
df.Revenue.is_monotonic_decreasing
df.Revenue.is_monotonic_increasing