简体   繁体   English

通过 Pandas 保持 Excel 下拉列表

[英]Keeping the Excel drop down list through pandas

I have an excel file where the headers have drop downs which can be used to select rows based on specific column values (exactly what WHERE statement does).我有一个 excel 文件,其中标题有下拉列表,可用于根据特定的列值(正是 WHERE 语句所做的)选择行。 I import this file into pandas and perform some operation.我将此文件导入熊猫并执行一些操作。 Let's say I drop duplicate values based on "emp_id" column"假设我根据“emp_id”列删除重复值”

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

Then I save this dataframe to an excel,然后我将此数据框保存到excel,

data.to_excel("new_data.xlsx")

However, this new data does not have any drop down on header.但是,此新数据在标题上没有任何下拉列表。 Is there way to retain the drop down or python/pandas does not support it?有没有办法保留下拉菜单或 python/pandas 不支持它?

If I understand you correctly, this can be done quite easily with XlsxWriter :如果我理解正确的话,这可以通过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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM