5

I'm reading some excel spreadsheets (xlsx format) into pandas using read_excel, which generally works great. The problem I have is that when a column contains numbers, pandas converts these to float64 type, and I would like them to be treated as strings. After reading them in, I can convert the column to str:

my_frame.my_col = my_frame.my_col.astype('str') 

This works as far as assigning the right type to the column, but when I view the values in this column, the strings are formatted in scientific-format e.g. 8.027770e+14, which is not what I want. I'd like to work out how to tell pandas to read columns as strings, or do the conversion later so that I get values in their original (non-scientific) format.

1
  • read_excel uses an external module, the dtypes will be embedded in your excel sheet so there is no dtype param as such you'd have to export as csv and use read_csv with dtype param or convert as a post-processing step Commented Feb 13, 2016 at 10:04

2 Answers 2

0

pandas.read_csv() has a dtype argument:

dtype : Type name or dict of column -> type

Data type for data or columns. E.g. {‘a’: np.float64, ‘b’: np.int32}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I could convert the excel files to csv format first using xlrd, although ideally I'd like to avoid this additional step.
0

I solve it with round, if you do round(number,5) in most case you will not lose data and you will get zero in the case of 8.027770e+14

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.