Pandas read_csv: AttributeError: 'NoneType' 对象没有属性 'dtype'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30383478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:23:00  来源:igfitidea点击:

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

pythoncsvpandas

提问by nilkn

Suppose I have a file test.csv which looks like this:

假设我有一个文件 test.csv ,它看起来像这样:

A,B,C
Hello,Hi,1

I'm attempting to read this into a Pandas dataframe:

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

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

This produces the error

这会产生错误

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

Any ideas?

有任何想法吗?

回答by chrisaycock

Your file already had the header row, so no need to specify any names.

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

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