简体   繁体   中英

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

I have a data frame like below 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.

How can this be done using python?

You can get what you want by using "xlsxwriter" engine. Please follow the below steps. I assumed "Invoice" is in column C. So, the output will be in Column 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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