pandas DtypeWarning:列 (15,16,18,24) 具有混合类型。如果列具有混合类型,则会被删除

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49575183/
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-14 05:23:57  来源:igfitidea点击:

DtypeWarning: Columns (15,16,18,24) have mixed types. Columns get removed if it has mixed type

pythonpandas

提问by Raj Kumar

I am trying to read a csv file

我正在尝试读取 csv 文件

pd.set_option('display.max_columns', None)
inventory = pd.read_csv('inventory-new.csv', sep=";", names=columns)

it says:

它说:

DtypeWarning: Columns (15,16,18,24) have mixed types. Specify dtype option on import or set low_memory=False.
interactivity=interactivity, compiler=compiler, result=result)

DtypeWarning:列 (15,16,18,24) 具有混合类型。在导入时指定 dtype 选项或设置 low_memory=False。
交互性=交互性,编译器=编译器,结果=结果)

and column numbers 15,16,18,24 gets completely removed

列号 15,16,18,24 被完全删除

I tried:

我试过:

inventory = pd.read_csv('inventory-new.csv', sep=";", names=columns, dtype=object)

also

inventory = pd.read_csv('inventory-new.csv', sep=";", names=columns, low_memory=False)

but the result is still the same. Why is this happening?

但结果还是一样。为什么会这样?

回答by michaelg

You need to set a dtype for each column.

您需要为每列设置一个 dtype。

From the doc:

文档

dtype : Type name or dict of column -> type, default None

Data type for data or columns. E.g. {‘a': np.float64, ‘b': np.int32} Use str or object to preserve and not interpret dtype. If converters are specified, they will be applied INSTEAD of dtype conversion.

dtype : 类型名称或列的字典 -> 类型,默认无

数据或列的数据类型。例如 {'a': np.float64, 'b': np.int32} 使用 str 或 object 来保留而不是解释 dtype。如果指定了转换器,它们将被应用于 dtype 转换的 INSTEAD。

Why it is happening ?

为什么会这样?

Most of the time, pandas try to figure out the dtype before processing rows. But if it happens that a value is not of the selected dtype, it will raise an error. Thus you will need to either correct the original data or choose a more permissive dtype to import (like you did with object).

大多数情况下,pandas 会在处理行之前尝试找出 dtype。但是如果碰巧某个值不是选定的 dtype,则会引发错误。因此,您需要更正原始数据或选择更宽松的 dtype 进行导入(就像您对 所做的那样object)。