Losing the last 4 numbers when saving dataframe to csv in python

Viewed 86

I am trying to save twitter data is being organized in an excel file (CSV file). To do this, I've written the following code:

df.to_csv(r'C:\Users\path\tweets.csv')

This code works fine and through Jupyter.

However, I am facing a problem when attempting to save this dataframe to a csv file. I'm losing the last 4 numbers of the values as follows.

on jupyter notebook: e.g., 1368350628343205888, 1368345015873835017 enter image description here

in excel: e.g., 1368350628343200000, 1368345015873830000 enter image description here

Hopefully, someone can help me with this problem. Many thanks !!

2 Answers

Issue is not in data, but Excel. Excel can only handle 15 digits. If you open your CSV with something like Notepad or VS Code, you'll see the digits are there in the raw data

Possible workarounds:

Import Number as Text

Code to Generate Sample CSV:

import pandas as pd
df = pd.DataFrame({"tweet_id":[1368350628343205888,1368345015873835017]})
df.to_csv('C:\\python_work_area\\test.csv',index=False)

setting excel column datatype to "text" may help. please check out the link below: set excel datatypes

Related