dandpes问题在pandas chunk read_csv中

时间:2017-01-05 17:59:13

标签: pandas

运行此代码以在块中加载csv时,

In [40]: chunk(4, l)
Out[40]: 
[[1, 2, 3, 4],
 [2, 3, 4, 5],
 [3, 4, 5, 6],
 [4, 5, 6, 7],
 [5, 6, 7, 8],
 [6, 7, 8, 9]]

In [41]: l = [1,2,3,4,5,6,7,8,9,10,11]

In [42]: chunk(16, l)
Out[42]: [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]]

它只返回基于对象。

df_i= pd.read_csv(file1,   sep=',')
dtype0= df_i.dtypes
list01= pd.read_csv(file1,  chunksize=chunksize, dtype= dtype0, sep=',')
for df_i in list01:
  print df_i.dtypes

而不是:

id                  object
chain               object
dept                object
category            object
company             object
dtype: object

这是一个熊猫错误/问题吗?

1 个答案:

答案 0 :(得分:1)

记录......

read_csv()函数的

dtype参数需要字典:{'col_name': <dtype>}

所以试着改变:

list01= pd.read_csv(file1,  chunksize=chunksize, dtype= dtype0, sep=',')

为:

list01= pd.read_csv(file1,  chunksize=chunksize, dtype= dtype0.to_dict(), sep=',')
相关问题