简体   繁体   中英

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(). 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. How do I resolve this issue, been stuck on it since 2 days. 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
  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.

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

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...

Here, the header is set to None , header=None , so no headers and the first raw will also be read as strings. If you do not want this, just do not include that part.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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