简体   繁体   English

使用 to_csv() 将 pandas 数据帧加载到 csv 文件时如何避免数据的数据类型转换

[英]How do we avoid the data type conversion of data when we load a pandas data frame into a csv file using to_csv()

To elaborate the issue what's happening is that we have a table in snowflake that is being loaded into a pandas data frame, we are then loading this data frame into a csv using to_csv().为了详细说明正在发生的问题,我们有一张雪花表正在加载到 pandas 数据帧中,然后我们使用 to_csv() 将此数据帧加载到 csv 中。 The thing is that there is one column in our data frame, let's say COL1 there is one value(and many such values) eg: 5MAR, this particular value while loading into the csv is getting converted to date ie 5-Mar, similarly 5JUL is getting converted to 5-Jul.问题是我们的数据框中有一列,假设 COL1 有一个值(和许多这样的值),例如:5MAR,加载到 csv 时的这个特定值被转换为日期,即 3 月 5 日,类似 5 月正在转换为 7 月 5 日。 How do I resolve this issue, been stuck on it since 2 days.我该如何解决这个问题,自 2 天以来一直卡在它上面。 Would really appreciate if someone would help me.如果有人能帮助我,我将不胜感激。

These are the following things I've tried:这些是我尝试过的以下事情:

  1. df.to_csv(csv_buffer, sep=",", quotechar='"', index=False, encoding='utf-8'). I've tried adding encoding = utf-8 but it is not working df.to_csv(csv_buffer, sep=",", quotechar='"', index=False, encoding='utf-8')。我尝试添加 encoding = utf-8 但它不起作用
  2. also tried changing the data type of that column to string(earlier it was object) but after the conversion using 'astype(str)' still the data type is object.还尝试将该列的数据类型更改为字符串(之前它是对象),但在使用“astype(str)”进行转换后,数据类型仍然是 object。

This may have been asked before one way or another... Anyway, it is highly possible the conversion is happening when you read in the first csv To load a csv, you want to use a这可能已经以一种或另一种方式被问过......无论如何,当您阅读第一个 csv 要加载 csv 时,很有可能正在发生转换。

df=pd.read_csv(
     csv_fname,header=None,
     converters={i: str for i in range(100)}
)

will make sure that each of the first 100 columns is read as a string...将确保前 100 列中的每一列都被读取为字符串...

Here, the header is set to None , header=None , so no headers and the first raw will also be read as strings.在这里, header 设置为 Noneheader=None ,因此没有标题,第一个 raw 也将被读取为字符串。 If you do not want this, just do not include that part.如果您不想要这个,请不要包含该部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM