8

我正在尝试处理保存到 CSV 的数据,这些数据可能在未知数量的列(最多约 30 个)中缺少值。我正在尝试使用genfromtxt'filling_missing参数将这些缺失值设置为 '0'。这是在 Win 7 上的 ActiveState ActivePython 2.7 32 位中运行的 numpy 1.6.2 的最小工作示例。

import numpy

text = "a,b,c,d\n1,2,3,4\n5,,7,8"
a = numpy.genfromtxt('test.txt',delimiter=',',names=True)
b = open('test.txt','w')
b.write(text)
b.close()
a = numpy.genfromtxt('test.txt',delimiter=',',names=True)
print "plain",a

a = numpy.genfromtxt('test.txt',delimiter=',',names=True,filling_values=0)
print "filling_values=0",a

a = numpy.genfromtxt('test.txt',delimiter=',',names=True,filling_values={1:0})
print "filling_values={1:0}",a

a = numpy.genfromtxt('test.txt',delimiter=',',names=True,filling_values={0:0})
print "filling_values={0:0}",a

a = numpy.genfromtxt('test.txt',delimiter=',',names=True,filling_values={None:0})
print "filling_values={None:0}",a

结果:

plain [(1.0, 2.0, 3.0, 4.0) (5.0, nan, 7.0, 8.0)]
filling_values=0 [(1.0, 2.0, 3.0, 4.0) (5.0, nan, 7.0, 8.0)]
filling_values={1:0} [(1.0, 2.0, 3.0, 4.0) (5.0, 0.0, 7.0, 8.0)]
filling_values={0:0} [(1.0, 2.0, 3.0, 4.0) (5.0, nan, 7.0, 8.0)]

Traceback (most recent call last):
  File "C:\Users\tolivo.EE\Documents\active\eng\python\sizer\testGenfromtxt.py", line 20, in <module>
    a = numpy.genfromtxt('test.txt',delimiter=',',names=True,filling_values={None:0})
  File "C:\Users\tolivo.EE\AppData\Roaming\Python\Python27\site-packages\numpy\lib\npyio.py", line 1451, in genfromtxt
    filling_values[key] = val
TypeError: list indices must be integers, not NoneType

从 NumPy 用户指南中,我希望filling_values=0并且filling_values={None:0}可以工作,但他们没有,并且分别抛出错误。当您指定正确的列 ( filling_values={1:0}) 时,它将起作用,但由于在用户选择之前我有大量未知数的列,我正在寻找自动设置填充值的方法,就像用户指南提示的那样。

I imagine I can probably count the columns in advance and create a dict to pass as the value to filling_values in the meantime, but is there a better way?

4

1 回答 1

8

It's not obvious from the documentation, but filling_values="0" works.

In [19]: !cat test.txt
a,b,c,d
1,2,3,4
5,,7,8
9,10,,12

In [20]: a = numpy.genfromtxt('test.txt', delimiter=',', names=True, filling_values="0")

In [21]: print a
[(1.0, 2.0, 3.0, 4.0) (5.0, 0.0, 7.0, 8.0) (9.0, 10.0, 0.0, 12.0)]
于 2013-02-28T21:31:07.030 回答