计算沿每一列的Pandas DataFrame自相关 [英] Calculating Autocorrelation of Pandas DataFrame along each Column
问题描述
我想计算Pandas DataFrame列中滞后长度为1的自相关系数.我的数据的摘要是:
I want to calculate the autocorrelation coefficients of lag length one among columns of a Pandas DataFrame. A snippet of my data is:
RF PC C D PN DN P
year
1890 NaN NaN NaN NaN NaN NaN NaN
1891 -0.028470 -0.052632 0.042254 0.081818 -0.045541 0.047619 -0.016974
1892 -0.249084 0.000000 0.027027 0.067227 0.099404 0.045455 0.122337
1893 0.653659 0.000000 0.000000 0.039370 -0.135624 0.043478 -0.142062
年,我想为每列( RF , PC 等)计算滞后一的自相关.
Along year, I want to calculate autocorrelations of lag one for each column (RF, PC, etc...).
为了计算自相关,我为开始和结束数据相差一年的每一列提取了两个时间序列,然后使用numpy.corrcoef
计算了相关系数.
To calculate the autocorrelations, I extracted two time series for each column whose start and end data differed by one year and then calculated correlation coefficients with numpy.corrcoef
.
例如,我写道:
numpy.corrcoef(data[['C']][1:-1],data[['C']][2:])
(整个DataFrame称为data
).
但是,不幸的是,该命令返回了:
(the entire DataFrame is called data
).
However, the command unfortunately returned:
array([[ nan, nan, nan, ..., nan, nan, nan],
[ nan, nan, nan, ..., nan, nan, nan],
[ nan, nan, nan, ..., nan, nan, nan],
...,
[ nan, nan, nan, ..., nan, nan, nan],
[ nan, nan, nan, ..., nan, nan, nan],
[ nan, nan, nan, ..., nan, nan, nan]])
有人可以建议我如何计算自相关吗?
Can somebody kindly advise me on how to calculate autocorrelations?
推荐答案
您应使用:
numpy.corrcoef(df['C'][1:-1], df['C'][2:])
df[['C']]
表示只有一列的数据框,而df['C']
是包含C列中值的系列.
df[['C']]
represents a dataframe with only one column, while df['C']
is a series containing the values in your C column.
这篇关于计算沿每一列的Pandas DataFrame自相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!