'Losing the last 4 numbers when saving dataframe to csv in python
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 !!
Solution 1:[1]
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:
- Use another tool other than Excel
- Use Excel's Data tab to Get External Data and import the tweet_id as Excel data type Text. Steps here: https://superuser.com/questions/1184658/getting-trailing-zero-in-csv-file-for-15-digit-number
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)
Solution 2:[2]
setting excel column datatype to "text" may help. please check out the link below: set excel datatypes
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Stephan |
| Solution 2 | Meelad Ghazipour |

