Sha*_*yag 3 python time-series correlation dataframe pandas
我有两个熊猫数据框,我只从一列中获取并将日期列设置为索引,所以现在我有两个系列。我需要找到这些 Series 的相关性。
以下是几行dfd:
index change
2018-12-31 -0.86
2018-12-30 0.34
2018-12-27 -0.94
2018-12-26 -1.26
2018-12-25 3.30
2018-12-24 -4.17
Run Code Online (Sandbox Code Playgroud)
来自dfp:
index change
2018-12-31 0.55
2018-12-30 0.81
2018-12-27 -2.99
2018-12-26 0.50
2018-12-25 3.59
2018-12-24 -3.43
Run Code Online (Sandbox Code Playgroud)
我试过:
correlation=dfp.corr(dfd)
Run Code Online (Sandbox Code Playgroud)
并得到以下错误:
TypeError: unsupported operand type(s) for /: 'str' and 'int'
Run Code Online (Sandbox Code Playgroud)
问题是dfp由数字的字符串表示填充,因此Series.astype用于转换为浮点数:
correlation=dfp.astype(float).corr(dfd.astype(float)
print (correlation)
0.8624789983270312
Run Code Online (Sandbox Code Playgroud)
如果某些非数字值解决方案 abaove 失败,则使用to_numericwith errors='coerce'- 非数字将转换为缺失值:
correlation=pd.to_numeric(dfp, errors='coerce').corr(dfd)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
74 次 |
| 最近记录: |