When I try to get my dataframe out of the csv file the type of the data changed. Is there a way I can avoid this?
CSVs are text files so don't inherently contain meta-data like datatypes.
When you're opening the CSV you've saved, Python/Pandas is interpreting what it thinks the datatypes are - presumably it's getting it slightly wrong compared to what you want (this is what's happening rather than your CSV being saved with the wrong datatypes).
Pandas 'read_csv' has a 'dtype' argument which will take a dictionary of column types. This should let you specify what data types you'd like. I'd suggest reading through the function documentation here.
2.1m questions
2.1m answers
60 comments
57.0k users