【问题标题】:pandas sparse data frame value_counts not working熊猫稀疏数据框 value_counts 不起作用
【发布时间】:2014-05-06 10:14:30
【问题描述】:

当我使用 value_counts 方法时,我遇到了带有 pandas 稀疏数据框的 TypeError。我已经列出了我正在使用的软件包的版本。

关于如何完成这项工作的任何建议?

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

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 pandas typeerror sparse-matrix


【解决方案1】:

这不是实现ATM,先转换成dense。

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

【讨论】:

  • 感谢您的快速回复。我有一个 7 毫米行和大约 7 毫米行的数据框。 90K 列。因此,为每个操作转换为密集的帧/数组是乏味的,并且会占用大量内存。
  • 欢迎您提出请求以添加此功能; sparse 需要一些 tlc
猜你喜欢
  • 1970-01-01
  • 2021-12-14
  • 2020-09-04
  • 2018-04-28
  • 1970-01-01
  • 1970-01-01
  • 2017-01-04
  • 1970-01-01
  • 2018-05-25
相关资源
最近更新 更多