Amu
Amu

Reputation: 23

To add number format to xlxs file using python

I have data like below in abc.xlxs

date,qty,price,profitprice,sellprice
20200501,11,900,,20

And using python I want output as:

data,qty,price,profitprice,sellprice
20200501,11.00,900.00,,20.00

Can any one help on this?

how can I read each column with its value and add number format and save to xlxs file?

Upvotes: 2

Views: 112

Answers (2)

Marcel
Marcel

Reputation: 402

Based on this answer by Akshit Khurana:

import pandas as pd

df = pd.read_excel("initial.xlsx")

writer = pd.ExcelWriter("formatted.xlsx", engine = "xlsxwriter")
df.to_excel(writer, index=False, header=True)
workbook  = writer.book
worksheet = writer.sheets['Sheet1']
format1 = workbook.add_format({'num_format': '0.00'})
worksheet.set_column('C:E', None, format1)  # Adds formatting to columns C-E
writer.save()

I believe the two other answers posted here do not work for the same reason why this question was asked.

Upvotes: 1

Bruno Mello
Bruno Mello

Reputation: 4618

You can use the dtype parameter at read_excel:

pd.read_excel('abc.xlxs', dtype={'profitprice': float, 'sellprice': float})

Upvotes: 0

Related Questions