How to keep the original data type and format in reading and writing dataframe?

Viewed 122

I have a dataframe column 'rating' with floats and one of its values is:

3.63043

My following code changed it to:

3.6304300000000000

The code is simply replace the sep ',' with '\t':

df = pd.read_csv('test.csv', encoding='utf8', sep=',')
#df = df.astype({"raing": str})
df.to_csv('test.2.csv', encoding='utf8', sep='\t', index=False)

If I use the commented out code to specify the rating as str. It not only still produces the very long float number, but also introduced a new problem. Some cells are empty originally, and now in the new test2.csv, those empty cells are 'nan' which isn't desired.

I just want to replace the ',' with '\t' and nothing else needs to be changed. What's the right way to do that?

1 Answers

For this simple task I would use standard csv module instead of pandas (that way nothing but delimiters are changed):

import csv

with open('test.csv', 'r') as f_in, open('test2.csv', 'w') as f_out:
    csv.writer(f_out, delimiter='\t').writerows(csv.reader(f_in, delimiter=','))
Related