[英]Calculate Yearly growth from monthly data
我有一个看起来像这样的数据框:
Station A B C
Date
2013-01-31 1340381 1568766 910785
2013-02-28 1261806 1447467 843956
2013-03-31 1399123 1579597 926968
2013-04-30 1395016 1618159 950947
2013-05-31 1340408 1654265 988293
这是每个月通过每个车站的总人数。 如何计算2013年哪个车站的增幅最大(旅行者数量增加)?
您可以进行线性回归并估算一年内的增长。 当然,由于您没有一年的数据价值,因此您会倾向于季节性变化。
import numpy as np
import pandas as pd
from scipy import stats
df = pd.DataFrame()
df['A'] = np.linspace(1000, 2000, 6)
df['B'] = np.linspace(1000, 3000, 6)
df['C'] = np.linspace(1000, 4000, 6)
df.index = pd.date_range('1/1/2015', periods=6, freq='M', name='Date')
print df
t = (pd.to_datetime(df.index.values) - pd.to_datetime('2015-01-01')).astype('timedelta64[D]')
slope, intercept, r_value, p_value, std_err = stats.linregress(t, df['A'])
print ('\nEstimated growth in one year of line A: ' + str(slope*365))
slope, intercept, r_value, p_value, std_err = stats.linregress(t, df['B'])
print ('\nEstimated growth in one year of line B: ' + str(slope*365))
slope, intercept, r_value, p_value, std_err = stats.linregress(t, df['C'])
print ('\nEstimated growth in one year of line C: ' + str(slope*365))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.