0

im reading an Excel file:

df = pd.read_excel(r'C:\test.xlsx', 'Sheet0', skiprows = 1)

The Excel file contains a column formatted General and a value like "405788", after reading this with pandas the output looks like "405788.0" so its converted as float. I need any value as String without changing the values, can someone help me out with this?

[Edit]

If i copy the values in a new Excel file and load this, the integers does not get converted to float. But i need to get the Values correct of the original file, so is there anything i can do?

Options dtype and converted changes the type as i need in str but as a floating number with .0

Shark00n
  • 85
  • 1
  • 10
  • 2
    Does this answer your question? [Python pandas: how to specify data types when reading an Excel file?](https://stackoverflow.com/questions/32591466/python-pandas-how-to-specify-data-types-when-reading-an-excel-file) – PApostol Nov 02 '21 at 16:53
  • Sadly does not work for me – Shark00n Nov 03 '21 at 12:10

1 Answers1

1

You can try to use the dtype attribute of the read_excel method.

df = pd.read_excel(r'C:\test.xlsx', 'Sheet0', skiprows = 1,
dtype={'Name': str, 'Value': str})

More information in the pandas docs: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_excel.html

intedgar
  • 631
  • 1
  • 11
  • Its get converted to String like i need, but before its get converted to an float so that it shows the number with .0 – Shark00n Nov 03 '21 at 12:16
  • Hm okay, weird. I tried it again and for me it works. I get back the exact same number with this approach but as a string without any .0 at the end. – intedgar Nov 03 '21 at 13:20
  • It must be any strange formatting in the Excel file i need to use, if i copy the values and paste them into a new Book, it works fine. – Shark00n Nov 03 '21 at 13:56