pandas 中的CORR函数出错 [英] Error with CORR function in Pandas

查看:147
本文介绍了 pandas 中的CORR函数出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含2个股票价格数据的CSV文件,我做了一些本地python编码,以创建2个股票柜台的2个价格单(以小数表示)。

I had a CSV file of 2 stock prices data which I did some native python coding to create 2 single lists of prices (in Decimals) of 2 stock counters.

然后我将它们转换为2个pandas数据帧,使用.pct_change()函数,然后应用a.corr(b)函数以尝试获得相关性。

I then converted them to 2 pandas dataframe, used the .pct_change() function then applied the a.corr(b) function in an attempt to get the correlation.

这是我得到的错误味精。任何人都可以在此问题上提供建议吗?

This is the error msg that I got. Anyone can advise on the issue here?

Traceback (most recent call last):
  File "D:/python/NQ_MSFT regression.py", line 71, in <module>
    print(nqpct.corr(mspct))
  File "C:\Anaconda3\lib\site-packages\pandas\core\frame.py", line 4431, in corr
    if method == 'pearson':
  File "C:\Anaconda3\lib\site-packages\pandas\core\generic.py", line 731, in __nonzero__
    .format(self.__class__.__name__))
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().


推荐答案

好吧,我意识到如果将列表解析为熊猫系列而不是数据框&将我的初始价格从字符串更改为浮动而不是小数。
为什么关联函数不能与十进制数据类型一起使用?

Ok I realised it works if I parse the list into a pandas series rather than a dataframe & change my initial prices from string to float rather than to decimals. Why doesn't the correlation function work with decimal data types though?

Traceback (most recent call last):
  File "D:/python/NQ_MSFT regression.py", line 64, in <module>
    nqpct = nq_df.pct_change()
  File "C:\Anaconda3\lib\site-packages\pandas\core\generic.py", line 4573, in pct_change
    axis=axis, **kwargs)) - 1)
  File "C:\Anaconda3\lib\site-packages\pandas\core\ops.py", line 866, in flex_wrapper
    return self._binop(other, op, level=level, fill_value=fill_value)
  File "C:\Anaconda3\lib\site-packages\pandas\core\series.py", line 1536, in _binop
    result = func(this_vals, other_vals)
TypeError: unsupported operand type(s) for /: 'decimal.Decimal' and 'float'

这篇关于 pandas 中的CORR函数出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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