问题
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]
查询成功。
本文探讨了在使用Pandas读取数据时遇到的数据类型警告问题,提供了两种解决方案:一是设置low_memory=False以允许Pandas在决定列类型前读取整个文件;二是明确指定混合类型列的数据类型,通过将特定列转换为字符串类型来解决问题。


被折叠的 条评论
为什么被折叠?



