DtypeWarning: Columns (1,5,7,16,......) have mixed types. Specify dtype option on import or set low_memory=False.
2023-09-14 09:08:35 时间
DtypeWarning: Columns (1,5,7,16,......) have mixed types. Specify dtype option on import or set low_memory=False. 意思就是:列1,5,7,16....的数据类型不一样。 调试进去看了看,发现pandas在读取的时候确实把同一列数据中同一 个数值识别为不同的类型,比如:2000行第3列值为0的数据识别为Int类型, 而在4000行第3列值为0的数据识别为str类型。
两种解决方法:
# 1.设置read_csv的dtype参数,指定字段的数据类型 pd.read_csv(sio, dtype={"user_id": int, "username": object}) # 2.设置read_csv的low_memory参数为False pd.read_csv(sio, low_memory=False})
注:low_memory
pandas读取csv文件默认是按块读取的,即不一次性全部读取;
另外pandas对数据的类型是完全靠猜的,所以pandas每读取一块数据就对csv字段的数据类型进行猜一次,所以有可能pandas在读取不同块时对同一字段的数据类型猜测结果不一致。
low_memory=False 参数设置后,pandas会一次性读取csv中的所有数据,然后对字段的数据类型进行唯一的一次猜测。这样就不会导致同一字段的Mixed types问题了。
但是这种方式真的非常不好,一旦csv文件过大,就会内存溢出;所以推荐用第1中解决方案。
另外pandas对数据的类型是完全靠猜的,所以pandas每读取一块数据就对csv字段的数据类型进行猜一次,所以有可能pandas在读取不同块时对同一字段的数据类型猜测结果不一致。
low_memory=False 参数设置后,pandas会一次性读取csv中的所有数据,然后对字段的数据类型进行唯一的一次猜测。这样就不会导致同一字段的Mixed types问题了。
但是这种方式真的非常不好,一旦csv文件过大,就会内存溢出;所以推荐用第1中解决方案。
pandas 更改数据类型:
选中列名,设置type
df[['two', 'three']] = df[['two', 'three']].astype(float)
相关文章
- How to deal with "Could not find component on update server. Contact VMware Support or your system administrator." in Vmware.
- The process could not execute 'sp_repldone/sp_replcounters' on 'ServerName'
- Hadoop - Kylin On OLAP1
- [服务器时区问题]PHP Warning: strftime(): It is not safe to rely on the system's timezone set
- [GraphQL] Filter Data Based on Query Arguments with GraphQL
- [Ramda] Filter an Array Based on Multiple Predicates with Ramda's allPass Function
- Spark On K8S 在有赞的实践与经验
- running Extension project directly on ABAP server without Launchpad
- Atitit q2016 qb doc list on home ntpc.docx
- 成功解决Fit Failed Warning: Estimator fit failed. The score on this train-test partition for these param
- 成功解决pandascoreframe.py:2754: SettingWithCopyWarning: A value is trying to be set on a copy of a s
- 标准C++类std::string的内存共享和Copy-On-Write...
- How to solve the problem : "You have been logged on with a temporary profile"
- java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
- 1127 ZigZagging on a Tree C++版
- SSO (Single Sign On)
- ORA-00845: MEMORY_TARGET not supported on this system