Skip to content
Advertisement

How to import all fields from xls as strings into a Pandas dataframe?

I am trying to import a file from xlsx into a Python Pandas dataframe. I would like to prevent fields/columns being interpreted as integers and thus losing leading zeros or other desired heterogenous formatting.

So for an Excel sheet with 100 columns, I would do the following using a dict comprehension with range(99).

JavaScript

These import files do have a varying number of columns all the time, and I am looking to handle this differently than changing the range manually all the time.

Does somebody have any further suggestions or alternatives for reading Excel files into a dataframe and treating all fields as strings by default?

Many thanks!

Advertisement

Answer

Try this:

JavaScript

UPDATE:

JavaScript
Advertisement