Home > Net >  Why pandastocsv does not write a proper value?
Why pandastocsv does not write a proper value?

Time:01-02

I'd like to write my dataframe to csv, the only column that has a problem is shipping tracking number. In the df data look like "SHP5047828131", but after write into csv I got "SHP 5047828131.00" how can I figure it out. In setting I just only set index = False, encoding='utf-8'. What Should I add something?

Here's my code below

import pandas as pd

List = ['SHP5047828131', 'SHP5047828678', 'SHP5047828625', 'SHP5047828926', 'SHP5047828767', 'SHP5047828679', 'SHP5047828952', '', 'SHP5047826824', 'SHP5047828384', 'SHP5047828856']
df = pd.DataFrame(zip(List))
print(df)

df.to_csv('/Users/sirilakkk./Desktop/test.csv', index = False, encoding='utf-8')

These two pic are the results first is from print directly and another is from csv file.

df data csv data

CodePudding user response:

You have misdiagnosed your problem. The file is being created correctly. If you cat test.csv or bring up the file in an editor like vi, you'll see that it just fine.

The issue you have is with Excel, which interprets SHP5047838131 as a currency value in Saint Helena Pounds. It is Excel that is interpreting the data as currency.

If you wait to avoid that, you need to have quotes around the fields in the CSV. You can have pandas do that by adding quoting=csv.QUOTE_ALL.

As a side note, you don't need zip if you only have one list.

import pandas as pd
import csv


List = ['SHP5047828131', 'SHP5047828678', 'SHP5047828625', 'SHP5047828926', 'SHP5047828767', 'SHP5047828679', 'SHP5047828952', '', 'SHP5047826824', 'SHP5047828384', 'SHP5047828856']
df = pd.DataFrame(List)
print(df)

df.to_csv('x.csv', index=False, encoding='utf-8', quoting=csv.QUOTE_ALL)
  • Related