Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PANDS稀疏数据帧值\u计数不起作用_Python_Pandas_Typeerror_Sparse Matrix - Fatal编程技术网

Python PANDS稀疏数据帧值\u计数不起作用

Python PANDS稀疏数据帧值\u计数不起作用,python,pandas,typeerror,sparse-matrix,Python,Pandas,Typeerror,Sparse Matrix,当我使用value_counts方法时,我遇到了数据帧稀疏的类型错误。我已经列出了我正在使用的软件包的版本 有没有关于如何使这项工作的建议 提前谢谢。另外,如果需要更多信息,请告诉我 Python 2.7.6 |Anaconda 1.9.1 (x86_64)| (default, Jan 10 2014, 11:23:15) [GCC 4.0.1 (Apple Inc. build 5493)] on darwin Type "help", "copyright", "credits" or

当我使用value_counts方法时,我遇到了数据帧稀疏的类型错误。我已经列出了我正在使用的软件包的版本

有没有关于如何使这项工作的建议

提前谢谢。另外,如果需要更多信息,请告诉我

Python 2.7.6 |Anaconda 1.9.1 (x86_64)| (default, Jan 10 2014, 11:23:15) 
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> import pandas
>>> print pandas.__version__
0.13.1
>>> import numpy
>>> print numpy.__version__
1.8.0

>>> dense_df = pandas.DataFrame(numpy.zeros((10, 10))
                               ,columns=['x%d' % ix for ix in range(10)])
>>> dense_df['x5'] = [1.0, 0.0, 0.0, 1.0, 2.1, 3.0, 0.0, 0.0, 0.0, 0.0]
>>> print dense_df['x5'].value_counts()
0.0    6
1.0    2
3.0    1
2.1    1
dtype: int64

>>> sparse_df = dense_df.to_sparse(fill_value=0) # Tried fill_value=0.0 also
>>> print sparse_df.density
0.04

>>> print sparse_df['x5'].value_counts()
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "//anaconda/lib/python2.7/site-packages/pandas/core/series.py", line 1156, in     value_counts
    normalize=normalize, bins=bins)
 File "//anaconda/lib/python2.7/site-packages/pandas/core/algorithms.py", line 231, in value_counts
    values = com._ensure_object(values)
  File "generated.pyx", line 112, in pandas.algos.ensure_object (pandas/algos.c:38788)
  File "generated.pyx", line 117, in pandas.algos.ensure_object (pandas/algos.c:38695)
  File "//anaconda/lib/python2.7/site-packages/pandas/sparse/array.py", line 377, in astype
    raise TypeError('Can only support floating point data for now')
TypeError: Can only support floating point data for now
Python 2.7.6 | Anaconda 1.9.1(x86_64)|(默认,2014年1月10日,11:23:15)
[GCC 4.0.1(苹果公司5493版)]关于达尔文
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>进口大熊猫
>>>打印熊猫版__
0.13.1
>>>进口numpy
>>>打印numpy.\uu版本__
1.8.0
>>>密集_df=pandas.DataFrame(numpy.zeros((10,10))
,列=['x%d'%ix,用于范围(10)]中的ix)
>>>密度_df['x5']=[1.0,0.0,0.0,1.0,2.1,3.0,0.0,0.0,0.0,0.0]
>>>打印密度密度密度密度[x5']。值计数()
零点零六
一点零二
三点零一
二点一一
数据类型:int64
>>>稀疏_df=密集_df.到_稀疏(填充值=0)#尝试填充值=0.0
>>>打印稀疏密度
0.04
>>>打印稀疏_df['x5']。值_计数()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“//anaconda/lib/python2.7/site packages/pandas/core/series.py”,第1156行,以值为单位
标准化=标准化,存储箱=存储箱)
文件“//anaconda/lib/python2.7/site packages/pandas/core/algorithms.py”,第231行,以值为单位
values=com.\u确保\u对象(值)
pandas.algos.sure_对象(pandas/algos.c:38788)中的文件“generated.pyx”,第112行
pandas.algos.sure_对象(pandas/algos.c:38695)中的文件“generated.pyx”,第117行
astype文件“//anaconda/lib/python2.7/site packages/pandas/sparse/array.py”,第377行
raise TypeError('目前只能支持浮点数据')
TypeError:目前只能支持浮点数据

这不是ATM实现的,先转换为密集型

In [12]: sparse_df['x5'].to_dense().value_counts()
Out[12]: 
0.0    6
1.0    2
3.0    1
2.1    1
dtype: int64

你是不是在家里养了只虫子。谢谢你的提示。谢谢你的快速回复。我有一个数据框,有7mm行和大约90K列。因此,将每个操作转换为密集的帧/数组非常繁琐,并且会占用大量内存;我需要一些tlc