2017-06-07 7 views
2

私はfloat_formatパラメータをパンダのto_excel()関数で動作させようとしていますが、何もしないようです。パンダ:to_excel()float_format

コード:

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ファイルは次のようになります。誰がどのように適切に知ってい

date  object 
numA float64 
numB float64 
dtype: object 

enter image description here

私のようにdtypesを確認書式浮動私はn to_excel()

答えて

3

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() 

結果:

Final excel result

さらに詳しい情報:http://xlsxwriter.readthedocs.io/example_pandas_column_formats.html

+0

あなたが正しいです。私は検索してきており、それが唯一の真の答えだと思われます。どちらがいいですか、私のアプローチを変えるだけです。ありがとうございました! – pshep123