簡體   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