简体   繁体   English

如何从 pandas dataframe 在 excel 中创建数据验证列?

[英]how to create a data validation column in excel from pandas dataframe?

I have a data frame like below df我有一个如下df的数据框

SL.No  Invoice     
1       A2345
2       B1624
3       C1234 

I need create another column Status with values such ['Approved',' Rejected','Partially Approved'] against each row to write into a excel file.我需要针对每一行创建另一列Status ,其值为 ['Approved','Rejected','Partially Approved'] 以写入 excel 文件。

How can this be done using python?如何使用 python 做到这一点?

You can get what you want by using "xlsxwriter" engine.你可以通过使用“xlsxwriter”引擎得到你想要的。 Please follow the below steps.请按照以下步骤操作。 I assumed "Invoice" is in column C.我假设“发票”在 C 列中。 So, the output will be in Column D.因此,output 将在 D 列中。

# Export to xlsx file
df.to_excel("c:/Error/DropDown.xlsx",engine='xlsxwriter') 

#Open it with xlsxwriter
writer = pd.ExcelWriter("c:/Error/DropDown.xlsx", engine='xlsxwriter') 
df.to_excel(writer, sheet_name='Sheet1')

#Assign the workbook and worksheet
workbook  = writer.book
worksheet = writer.sheets['Sheet1']

#Adding the header and Datavalidation list
worksheet.write('D1', 'Status')
worksheet.data_validation('D2', {'validate': 'list',
                                  'source': ['Approved',' Rejected','Partially Approved']})
workbook.close()

Document to refer: xlsxwriter_Dropdown参考文档: xlsxwriter_Dropdown

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

相关问题 熊猫:根据数据和列顺序创建数据框 - Pandas: Create dataframe from data and column order 如何使用来自另一列的数据创建新的Pandas Dataframe列 - How do I create a new Pandas Dataframe Column with data from another column 如何从数据框中的其他列创建新的Pandas数据框列 - How to create a new Pandas dataframe column from other columns in the dataframe 如何通过拆分从数据框中的另一列创建 Pandas 数据框? - How to create a Pandas dataframe from another column in a dataframe by splitting it? 通过xlwings将数据从pandas中的数据帧写入Excel时如何跳过默认索引(左侧的系列列)? - How to skip default index (series column on the left) when writing data from dataframe in pandas into Excel through xlwings? 如何从 pandas dataframe 中的现有列创建新列 - How to create a new column from an existing column in a pandas dataframe 如何从熊猫数据框中的特定列编写多个 Excel 表? - How to write multiple excel sheets from a particular column in a pandas dataframe? 在 dataframe pandas 中获取列的名称并从中创建数据 - Get the name of a column and create data from it in dataframe pandas Pandas Dataframe 列验证 - Pandas Dataframe column Validation Python如何从熊猫数据框和openpyxl使用自动过滤器创建excel - Python how to create excel with autofilter from pandas dataframe and openpyxl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM