python-3.x - 通过 pandas 保留 Excel 下拉列表

标签 python-3.x pandas excel-2013

我有一个 Excel 文件,其中标题有下拉菜单,可用于根据特定列值选择行(正是 WHERE 语句的作用)。我将此文件导入 pandas 并执行一些操作。假设我根据“emp_id”列删除重复值

data = data.drop_duplicates(['emp_id'])

然后我将此数据框保存到 Excel 中,

data.to_excel("new_data.xlsx")

但是,这个新数据的标题上没有任何下拉菜单。有没有办法保留下拉菜单或者python/pandas不支持它?

最佳答案

如果我理解正确的话,这可以通过 XlsxWriter 轻松完成。 :

import pandas as pd

df = pd.DataFrame({
    'Numbers': [1, 2, 3, 4, 5],
    'Letters': ['a', 'b', 'c', 'd', 'e']
})

with pd.ExcelWriter('new_data.xlsx', engine='xlsxwriter') as writer:
    df.to_excel(excel_writer=writer, sheet_name='Filter', index=False)

    worksheet = writer.sheets['Filter']

    # set up autofilter
    worksheet.autofilter(0, 0, len(df.index) - 1, len(df.columns) - 1)

关于python-3.x - 通过 pandas 保留 Excel 下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59753710/

相关文章:

excel - 根据标准标记项目

VBA:将多个值传递给 Instr

mysql - pymysql查询: unable to rollback

python - 词频硬件

python-3.x - 如何在两个矩阵的比较中允许任何列的符号差异,Python?

python - Pandas:根据值将包含分号的列分隔为多列

python - 剥离/修剪数据帧的所有字符串

mysql - python执行sql请求时一直报错

python - 如何有条件地跳过pd.read_html()中不包含表的html文件?

Excel.Application.Cells.SpecialCells(xlCellTypeLastCell) 返回工作表的底部,而不是最后一个数据单元格