Raf*_*eaz 5 python python-3.x pandas xlsxwriter
我有一个数据框,我正在使用 xlsxwriter 将其写入 excel,并且我希望将自动过滤器应用于我的电子表格中标题不是空白的所有列,而无需指定范围(例如 A1:D1)。有没有办法做到这一点?
您将需要以某种方式指定范围,但您可以根据数据框的shape()以编程方式进行。
例如:
import xlsxwriter
import pandas as pd
df = pd.DataFrame({'A' : [1, 2, 3, 4, 5, 6, 7, 8],
'B' : [1, 2, 3, 4, 5, 6, 7, 8],
'C' : [1, 2, 3, 4, 5, 6, 7, 8],
'D' : [1, 2, 3, 4, 5, 6, 7, 8]})
writer = pd.ExcelWriter('test.xlsx', engine = 'xlsxwriter')
# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1')
# Get the xlsxwriter objects from the dataframe writer object.
workbook = writer.book
worksheet = writer.sheets['Sheet1']
# Apply the autofilter based on the dimensions of the dataframe.
worksheet.autofilter(0, 0, df.shape[0], df.shape[1])
workbook.close()
writer.save()
Run Code Online (Sandbox Code Playgroud)
输出:
| 归档时间: |
|
| 查看次数: |
1885 次 |
| 最近记录: |