How to prevent integers from being converted to floats when converting a data frame to a list?
I have a .csv file with 5 columns of data. The first four columns have no decimal points, while the last column does.
When I import this data into my script using "pd.read_csv", the data imports correctly, with the first 4 numbers as integers and the last as a float, like this:
1,1,10,0,1.0
1,1,11,0,0.6
1,1,12,0,0.0
BUT I need to convert this data into a list, and when I do it converts all the numbers into floats. I do not want this. The first four values need to be integers.
This is my current code, which, after its is converted to a list, provides a list where all numbers are float:
data_file_name = r'C:\Users\username\Desktop\FileName.csv'
data = pd.read_csv(data_file_name) #<This part works and the data types are correct, the first 4 are integers
data2 = data.values.tolist() #<here is where everything gets converted to a float, even if it was defined as an int in the df.
This results in a list with the data formatted like this:
[[1.0, 1.0, 10.0, 0.0, 1.0], [1.0, 1.0, 11.0, 0.0, 0.6], [1.0, 1.0, 12.0, 0.0, 0.0]]
When I need it to be formatted like this:
[[1, 1, 10, 0, 1.0], [1, 1, 11, 0, 0.6], [1, 1, 12, 0, 0.0]]
What can I do?
I've tried:
[int(i,10) for i in data]
But this returns this error:
ValueError: invalid literal for int() with base 10: 'Month'