Python3:解决DtypeWarning: mixed types.

问题

pandas读入数据时,出现如下数据类型warning

DtypeWarning: Columns (0) have mixed types. Specify dtype option on import or set low_memory=False.
  exec(code_obj, self.user_global_ns, self.user_ns)

这会导致后续在DataFrame中检索数据失败,例如df['col'].isin(['abcde'])输出为空。

方法

法1:

按照提示,读入数据时指定参数low_memory=False,可以部分解决这类问题。

How exactly does low_memory=False fix the problem?

It reads all of the file before deciding the type, therefore needing more memory.

法2:

为出现混合类型(mixed types)的列,指定类型。

首先,根据“Columns (0) have mixed types”可以判断是columns[0]出现混合类型;

然后,进行如下设定:

In [68]: df_temp = df_temp.astype({df_temp.columns[0]: str})

In [68]: df_temp[df_temp['record_index']=='201904290059392698']
Out[68]: 
             record_index         p_id     ...     eqp_id operator_id
28772  201904290059392698  940071C2B01     ...      ECR03    39091876

[1 rows x 7 columns]

查询成功。

发布了28 篇原创文章 · 获赞 5 · 访问量 4075

猜你喜欢

转载自blog.csdn.net/authorized_keys/article/details/90173636