0

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,
1
  • 2
    Show us a couple lines of the csv along with their unexpected output. Commented Feb 20, 2015 at 8:39

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.