1

I have a DataFrame that contains an A/C number column, where some values are longer than 15 digits. When I export the DataFrame to Excel using .to_excel(), Excel automatically converts these long numbers into scientific notation or rounds them off (e.g., showing 000 after 15 digits). How can I export this column as text, so the full account numbers are preserved in Excel?

enter image description here

2
  • I would add a prefix to the account number similar to that used for hexadecimal It may seem clumsy but trying to represent the account number as numeric is asking for trouble. Commented Nov 11 at 8:47
  • 1
    You could convert the column to category dtype with pandas before exporting to Excel. Another option would be concatenating a single quote to value before exporting. As example, value 1234 would be '1234. The ' in Excel forces the program to read it as text. Commented Nov 11 at 9:12

2 Answers 2

2

You may specify the data type explicitly using ExcelWriter openpyxl and convert only certain columns to strings:

with pd.ExcelWriter('output.xlsx', engine='openpyxl') as writer:
    df.astype({'ZipCode': str}).to_excel(writer, index=False)
Sign up to request clarification or add additional context in comments.

Comments

0

I would strongly suggest using

df.to_csv("mydata.csv", index=False)

This would display your data in an Excel sheet, where df here would be the name of the data frame.

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.