亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

帶有 Pandas 數據框千位分隔符的 XlsxWriter

帶有 Pandas 數據框千位分隔符的 XlsxWriter

拉風的咖菲貓 2022-06-02 15:42:00
據我所知,Xlsxwriter 可能是用千位分隔符格式化我的數字的最佳軟件包。我已經多次閱讀 xlsxwriter 文檔,仍然很困惑,我認為其他人可能有同樣的問題,因此我在這里發布我的問題。我有一個熊貓數據框 DF_T_1_EQUITY_CHANGE_Summary_ADE,我想將它們導出到 excel 并使用格式化千位分隔符。Row Labels               objectSum of EQUITY_CHANGE    float64Sum of TRUE_PROFIT      float64Sum of total_cost       float64Sum of FOREX VOL        float64Sum of BULLION VOL      float64Oil                     float64Sum of CFD VOL           objectSum of BITCOIN VOL       objectSum of DEPOSIT          float64Sum of WITHDRAW         float64Sum of IN/OUT           float64dtype: object數據框 DF_T_1_EQUITY_CHANGE_Summary_ADE 很清楚,除了第一列行標簽是對象,其他都是數字。因此,我使用 xlsxwriter 將數據框寫入 excel:import xlsxwriter num_fmt = workbook.add_format({'num_format': '#,###'}) #set the separator I wantwriter = pd.ExcelWriter('ADE_CN.xlsx', engine='xlsxwriter')DF_T_1_EQUITY_CHANGE_Summary_ADE.to_excel(writer, sheet_name='Sheet1')workbook=writer.bookworksheet = writer.sheets['Sheet1']worksheet.set_column('C:M', None, num_fmt)writer.save()但是,我沒有得到千位分隔符,excel中的結果如下:    Row Labels  Sum of EQUITY_CHANGE    Sum of TRUE_PROFIT  Sum of total_cost   Sum of FOREX VOL    Sum of BULLION VOL  Oil Sum of CFD VOL  Sum of BITCOIN VOL  Sum of DEPOSIT  Sum of WITHDRAW Sum of IN/OUT0   ADE A BOOK USD  778.17  517.36  375.9   37.79   0.33    0   0   0   1555.95 0   1555.951   ADE B BOOK USD  6525.51 403.01  529.65  35.43   14.3    0   0   0   500 -2712.48    -2212.482   ADE A BOOK AUD  537.7   189.63  147 12.25   0   0   0   0   0   0   03   ADE B BOOK AUD  -22235.71   7363.14 224.18  2.69    9.16    0.2 0   0   5000    -103    4897有人可以提供解決方案,非常感謝。
查看完整描述

1 回答

?
MMMHUHU

TA貢獻1834條經驗 獲得超8個贊

它應該工作。在獲得對工作簿對象的引用后,您需要add_format()稍后在代碼中移動它。這是一個例子:


import pandas as pd



# Create a Pandas dataframe from some data.

df = pd.DataFrame({'Data': [1234.56, 234.56, 5678.92]})


# Create a Pandas Excel writer using XlsxWriter as the engine.

writer = pd.ExcelWriter('pandas.xlsx', engine='xlsxwriter')


# Convert the dataframe to an XlsxWriter Excel object.

df.to_excel(writer, sheet_name='Sheet1')


# Get the xlsxwriter workbook and worksheet objects.

workbook  = writer.book

worksheet = writer.sheets['Sheet1']


# Set a currency number format for a column.

num_format = workbook.add_format({'num_format': '#,###'})

worksheet.set_column('B:B', None, num_format)


# Close the Pandas Excel writer and output the Excel file.

writer.save()


輸出:

http://img1.sycdn.imooc.com//629869e20001324503530236.jpg

查看完整回答
反對 回復 2022-06-02
  • 1 回答
  • 0 關注
  • 159 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號