繁体   English   中英

熊猫read_csv:AttributeError:“ NoneType”对象没有属性“ dtype”

[英]Pandas read_csv: AttributeError: 'NoneType' object has no attribute 'dtype'

假设我有一个文件test.csv,如下所示:

A,B,C
Hello,Hi,1

我正在尝试将其读入Pandas数据框:

cols = ['A','B','C']
col_types = {'A': str, 'B': str, 'C': int}
test = pd.read_csv('test.csv', names=cols, dtype=col_types)

这会产生错误

AttributeError: 'NoneType' object has no attribute 'dtype'

有任何想法吗?

您的文件已包含标题行,因此无需指定任何名称

In [6]: test = pd.read_csv('test.csv', dtype=col_types)

In [7]: test
Out[7]:
       A   B  C
0  Hello  Hi  1

In [8]: test.dtypes
Out[8]:
A    object
B    object
C     int64
dtype: object

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM