带有 Pandas 数据帧千位分隔符的 XlsxWriter
XlsxWriter with Pandas dataframe thousand separator
据我所知,Xlsxwriter 可能是用千位分隔符格式化我的数字的最佳软件包。 xlsxwriter的文档我看了很多遍,还是很迷惑,我想其他人可能也有同样的问题,所以我post我的问题在这里。我有一个 pandas 数据框 DF_T_1_EQUITY_CHANGE_Summary_ADE,我想将它们导出到 excel 并使用格式化千位分隔符。
Row Labels object
Sum of EQUITY_CHANGE float64
Sum of TRUE_PROFIT float64
Sum of total_cost float64
Sum of FOREX VOL float64
Sum of BULLION VOL float64
Oil float64
Sum of CFD VOL object
Sum of BITCOIN VOL object
Sum of DEPOSIT float64
Sum of WITHDRAW float64
Sum of IN/OUT float64
dtype: object
dataframe DF_T_1_EQUITY_CHANGE_Summary_ADE除了第一列Row Labels是object,其他都是数字,其他都清楚。
所以,我使用 xlsxwriter 将数据帧写入 excel:
import xlsxwriter
num_fmt = workbook.add_format({'num_format': '#,###'}) #set the separator I want
writer = pd.ExcelWriter('ADE_CN.xlsx', engine='xlsxwriter')
DF_T_1_EQUITY_CHANGE_Summary_ADE.to_excel(writer, sheet_name='Sheet1')
workbook=writer.book
worksheet = 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/OUT
0 ADE A BOOK USD 778.17 517.36 375.9 37.79 0.33 0 0 0 1555.95 0 1555.95
1 ADE B BOOK USD 6525.51 403.01 529.65 35.43 14.3 0 0 0 500 -2712.48 -2212.48
2 ADE A BOOK AUD 537.7 189.63 147 12.25 0 0 0 0 0 0 0
3 ADE B BOOK AUD -22235.71 7363.14 224.18 2.69 9.16 0.2 0 0 5000 -103 4897
谁能提供解决方案,不胜感激。
应该可以。在获得对工作簿对象的引用后,您需要稍后在代码中移动 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()
输出:
据我所知,Xlsxwriter 可能是用千位分隔符格式化我的数字的最佳软件包。 xlsxwriter的文档我看了很多遍,还是很迷惑,我想其他人可能也有同样的问题,所以我post我的问题在这里。我有一个 pandas 数据框 DF_T_1_EQUITY_CHANGE_Summary_ADE,我想将它们导出到 excel 并使用格式化千位分隔符。
Row Labels object
Sum of EQUITY_CHANGE float64
Sum of TRUE_PROFIT float64
Sum of total_cost float64
Sum of FOREX VOL float64
Sum of BULLION VOL float64
Oil float64
Sum of CFD VOL object
Sum of BITCOIN VOL object
Sum of DEPOSIT float64
Sum of WITHDRAW float64
Sum of IN/OUT float64
dtype: object
dataframe DF_T_1_EQUITY_CHANGE_Summary_ADE除了第一列Row Labels是object,其他都是数字,其他都清楚。 所以,我使用 xlsxwriter 将数据帧写入 excel:
import xlsxwriter
num_fmt = workbook.add_format({'num_format': '#,###'}) #set the separator I want
writer = pd.ExcelWriter('ADE_CN.xlsx', engine='xlsxwriter')
DF_T_1_EQUITY_CHANGE_Summary_ADE.to_excel(writer, sheet_name='Sheet1')
workbook=writer.book
worksheet = 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/OUT
0 ADE A BOOK USD 778.17 517.36 375.9 37.79 0.33 0 0 0 1555.95 0 1555.95
1 ADE B BOOK USD 6525.51 403.01 529.65 35.43 14.3 0 0 0 500 -2712.48 -2212.48
2 ADE A BOOK AUD 537.7 189.63 147 12.25 0 0 0 0 0 0 0
3 ADE B BOOK AUD -22235.71 7363.14 224.18 2.69 9.16 0.2 0 0 5000 -103 4897
谁能提供解决方案,不胜感激。
应该可以。在获得对工作簿对象的引用后,您需要稍后在代码中移动 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()
输出: