【发布时间】:2016-07-11 18:23:01
【问题描述】:
尝试使用以下格式将 csv 文件读入 pandas 数据框
dp = pd.read_csv('products.csv', header = 0, dtype = {'name': str,'review': str,
'rating': int,'word_count': dict}, engine = 'c')
print dp.shape
for col in dp.columns:
print 'column', col,':', type(col[0])
print type(dp['rating'][0])
dp.head(3)
这是输出:
(183531, 4)
column name : <type 'str'>
column review : <type 'str'>
column rating : <type 'str'>
column word_count : <type 'str'>
<type 'numpy.int64'>
我可以理解 pandas 可能会发现很难将字典的字符串表示形式转换为给定 this 和 this 的字典。但是“评分”列的内容怎么可能同时是str和numpy.int64???
顺便说一句,诸如不指定引擎或标头之类的调整不会改变任何东西。
感谢和问候
【问题讨论】:
标签: python csv dictionary pandas types