0

I have data from csv:

36849|17|4.7|20180118103240
36792|17|5.3|20180118103238

4.7 and 5.3 is float

But when I do like this:

scores_data_train = pd.read_csv('../Dataset/TrainData//u.score.csv', sep='|')
scores_train = scores_data_train.as_matrix()
print(scores_train[:1, :])

The result:

[[3.68490000e+04 1.70000000e+01 4.70000000e+00 2.01801181e+13]]

Please help me. Thank you

1
  • Why are you using as_matrix? What's the intention here? It looks like the result is exactly correct. What is the problem? Commented Apr 1, 2019 at 16:02

2 Answers 2

0

The as_matrix() method turns the dataframe into a numpy array, which is limited to a single datatype by definition. You can't have some of the elements treated as floats while others are integers.

As long as you do not call .as_matrix(), you will have a dataframe, which can have integer and float columns. The types for each column can be specified by calling pd.read_csv(..., dtype={"colname": "int", "colname2": "float"}).

Sign up to request clarification or add additional context in comments.

Comments

0

Please do the below settings after importing numpy. refer similar questions, please check

   import numpy as np  
   np.set_printoptions(suppress=True,
   formatter={'float_kind':'{:0.2f}'.format}) 

#float, 2 units #precision right, 0 on left

3 Comments

The pandas option will not affect the output format of the numpy array returned by .as_matrix() - after that point, it's basically out of pandas's hands.
idea was to use it before converting to a matrix. Will that not work ?
Unfortunately not - the option only takes effect when floats inside a pandas dataframe are formatted to strings. Calling .as_matrix() will turn the dataframe into an array of floats, so pandas display formatting doesn't come into play.

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.