Olivier
Olivier

Reputation: 2111

pandas.read_csv reading string instead of float

My program keeps reading the input file as a string, even though it all values are floats.

pd.read_csv('input.txt', sep=' ', dtype=np.float32)

Also, my array contains multiple dots in the float values for some reason, even though the format is fine in my text

input.txt content:

-0.90051 -0.90051 -1.071287 -1.098813 -1.023997 -0.90051 -1.187293

result of pd.read_csv('input.txt', sep=' ', dtype=np.float32)

-0.90051, -0.90051.1, -1.071287, -1.098813, -1.023997, -0.90051.2 -1.187293,

Upvotes: 0

Views: 13027

Answers (1)

EdChum
EdChum

Reputation: 393963

You have not told read_csv that you have no header line hence you're observing the additional decimal points as the names clash, pass header=None to read_csv:

In [354]:
# your code
temp='''-0.90051 -0.90051 -1.071287 -1.098813 -1.023997 -0.90051 -1.187293'''
pd.read_csv(io.StringIO(temp), sep=' ', dtype=np.float32)
Out[354]:
Empty DataFrame
Columns: [-0.90051, -0.90051.1, -1.071287, -1.098813, -1.023997, -0.90051.2, -1.187293]
Index: []

In [355]:
# pass header=None
pd.read_csv(io.StringIO(temp), sep=' ', header=None, dtype=np.float32)
Out[355]:
         0        1         2         3         4        5         6
0 -0.90051 -0.90051 -1.071287 -1.098813 -1.023997 -0.90051 -1.187293

Upvotes: 2

Related Questions