简体   繁体   中英

How to add a empty column to a specific sheet in excel using panda?

I have an excel file that contains 3 sheets (PizzaHut, InAndOut, ColdStone). I want to add an empty column to the InAndOut sheet.

path = 'C:\\testing\\test.xlsx'
data = pd.ExcelFile(path)
sheets = data.sheet_names
if 'InAndOut' in sheets:
     something something add empty column called toppings to the sheet
data.to_excel('output.xlsx')

Been looking around, but I couldn't find an intuitive solution to this.

Any help will be appreciated!

  1. Read in the sheet by name.
  2. Do what you need to do.
  3. Overwrite the sheet with the modified data.
sheet_name = 'InAndOut'

df = pd.read_excel(path, sheet_name)

# Do whatever

with pd.ExcelWriter(path, engine="openpyxl", mode="a", if_sheet_exists="replace") as writer:
    df.to_excel(writer, sheet_name, index=False)

See pd.read_excel and pd.ExcelWriter .

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