【问题标题】:Pandas read_csv() gives DtypeWarning熊猫 read_csv() 给出 DtypeWarning
【发布时间】:2018-07-19 08:37:01
【问题描述】:

我从如下数据框创建了一个.csv 文件:

df.to_csv('partial.csv', sep=',')

数据框中的数据类型

df.dtypes 给出:

Contact_ID      int64
Src_Sys_Cd     object
First_Name     object
Last_Name      object
Src_Sys_Key    object
Full_Name      object
Office_No      object
Mobile         object
Email          object
dtype: object

当我尝试使用read_csv读取新创建的.csv文件时,出现错误:

new_df =  pd.read_csv('partial.csv')

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

如何避免此错误?出现此错误是因为我在 to_csvread_csv 时做错了吗?

【问题讨论】:

标签: python pandas


【解决方案1】:

请试一试下面的内容。它可能会很好,

new_df = pd.read_csv('partial.csv', low_memory=False)

【讨论】:

  • low_memory = False 是做什么的?
  • @TomJMuthirenthi 来自文档Internally process the file in chunks, resulting in lower memory use while parsing, but possibly mixed type inference. To ensure no mixed types either set False, or specify the type with the dtype parameter. Note that the entire file is read into a single DataFrame regardless, use the chunksize or iterator parameter to return the data in chunks. (Only valid with C parser)
【解决方案2】:

您的原始列的数据类型是什么? 您可以尝试通过输入参数 dtype 来指定 read_csv 中的数据类型:

types = {‘your_col_name01’: your_dtype01, ‘your_col_name02’: your_dtype02}
new_df = pd.read_csv('partial.csv', dtype=types)

【讨论】:

  • 我已经在问题中添加了数据类型
猜你喜欢
  • 2018-01-27
  • 2016-03-30
  • 2019-07-12
  • 2017-08-02
  • 1970-01-01
  • 2012-11-21
  • 2019-12-31
  • 2016-02-23
  • 2020-08-24
相关资源
最近更新 更多