DtypeWarning:列(13)具有混合类型

2024-10-03 09:11:03 发布

您现在位置:Python中文网/ 问答频道 /正文

当我使用指定为object的列运行以下代码时,我得到了警告

/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py:2785: DtypeWarning: Columns (13) have mixed types. Specify dtype option on import or set low_memory=False.
  interactivity=interactivity, compiler=compiler, result=result)

我还尝试设置low_memory=False,并使用假低内存和指定数据类型,但错误不会消失

最奇怪的是,我在另外两个文件上使用了完全相同的代码,但没有出现错误

我能做什么

这是所有3个文件中最大的一个。它有超过400000行和14列。其他的有200000多行和170000多行

re = requests.get('https://s3.amazonaws.com/baywheels-data/202002-baywheels-tripdata.csv.zip')
zip_file = zipfile.ZipFile(io.BytesIO(re.content))
csvfile = zip_file.open('202002-baywheels-tripdata.csv')

df = pd.read_csv(csvfile)
df.to_csv('Ford02-2020.csv',index=False)

df2 = pd.read_csv('Ford02-2020.csv', dtype={'rental_access_method': object})
df2.head()

来自列'rental_access_method'的示例数据

0    NaN
1    NaN
2    NaN
3    NaN
4    NaN
...
432349    app
432350    app
432351    app
432352    app
432353    app
Name: rental_access_method, dtype: object

Tags: csv代码falseappobjectaccessnanzip