我试图让float_format
参数与pandas'to_excel()
函数一起使用,但它似乎没有做任何事情。
代码:
df = pd.DataFrame({
'date':['1/15/2016','2/1/2016','2/15/2016','3/15/2016'],
'numA':[1000,2000,3000,4000.3],
'numB':[10000,20000.2,30000,40000]
})
writer = pd.ExcelWriter('c:/.../pandas_excel_test.xlsx', engine = 'xlsxwriter')
print df.dtypes
df.to_excel(writer,
index = False,
float_format = '%.2f',
)
但Excel文件如下所示:
我确认dtypes为:
date object
numA float64
numB float64
dtype: object
有谁知道如何正确格式化to_excel()
中的花车?
答案 0 :(得分:4)
我相信Excel格式会改变浮动的显示方式。我尝试了to_csv
方法并且float_format
工作了。对于excel,告诉excel如何显示列有助于:
df = pd.DataFrame({
'date':['1/15/2016','2/1/2016','2/15/2016','3/15/2016'],
'numA':[1000,2000,3000,4000.3],
'numB':[10000,20000.2,30000,40000]
})
writer = pd.ExcelWriter('c:/.../pandas_excel_test.xlsx', engine = 'xlsxwriter')
df.to_excel(writer, index=False, sheet_name='Sheet1')
workbook = writer.book
worksheet = writer.sheets['Sheet1']
format1 = workbook.add_format({'num_format': '0.00'})
worksheet.set_column('C:C', None, format1) # Adds formatting to column C
writer.save()
结果:
更多信息:http://xlsxwriter.readthedocs.io/example_pandas_column_formats.html