Evan Cole
Evan Cole

Reputation: 53

pandas.to_csv outputs a column of floats instead of integers

I'm working on project involving querying data from a dataframe, performing a few operations on it and then storing it in a csv. Here is the stripped-down code.

get_value() is a function which returns the mean of five values gotten from a query, forced into int.

import pandas as pd
d = pd.DataFrame(columns=['"Column1"','"Column2"'])
test = pd.read_csv("./test.csv", header = None, low_memory=False)
for line in range(1, 15):
    if test.values[line][5] == '1':
        value = str(get_value(line, 1))
    else:
        value = str(get_value(line, 0))
    d.loc[line-1]=[line,value]
d.to_csv('output.csv', index = False)

Unfortunately, whenever I do so I get the first column (line, obviously an integer here) as a series of floats. Sample output:

1.0,4859
2.0,7882
3.0,10248
4.0,8098
5.0,8048
6.0,6087
7.0,7349
8.0,8246
9.0,5863
10.0,5962
11.0,7641
12.0,8127
13.0,7808
14.0,9886

Replacing the to_csv with a print statement gives me a dataframe full of beautiful ints:

0      1    4859
1      2    7882
2      3   10248
3      4    8098
4      5    8048
5      6    6087
6      7    7349
7      8    8246
8      9    5863
9     10    5962
10    11    7641
11    12    8127
12    13    7808
13    14    9886

As a result I suspect it's got something to do with to_csv, but I'm a novice and far from certain about that. What's going on, and is there any workaround? Thanks for reading.

Edit: DSM has helpfully suggested I run d.info(). It looks like he's right, and that they're int-looking floats.

Int64Index: 14 entries, 0 to 13
Data columns (total 2 columns):
"Id"       14 non-null float64
"Sales"    14 non-null object

Upvotes: 5

Views: 9858

Answers (1)

JAB
JAB

Reputation: 12781

you can change the 'floats' to 'int' via the 'astype' method:

df['id'] =df['id'].astype(int)

Upvotes: 10

Related Questions