简体   繁体   中英

Update a single cell in an Excel spreadsheet using Pandas

I'm just wondering how to update a single cell in an excel spreadsheet with Pandas in a python script. I don't want any of the other cells in the file to be overwritten, just the one cell I'm trying to update. I tried using.at[], .iat[], and.loc() but my excel spreadsheet does not update. None of the other deprecated methods like.set_value() work either. What am I doing wrong?

import pandas as pd

tp = pd.read_excel("testbook.xlsx", sheet_name = "Sheet1")
tp.at[1, 'A'] = 10

I might suggest using xlwings for this operation, as it might be easier than reading and writing a sheet in pandas dataframes. The example below changes the value of "A1".

import xlwings as xw

sheet = xw.Book("testbook.xlsx").sheets("Sheet1")
sheet.range("A1").value = "hello world"

Also note xlwings is included with all Anaconda packages if you're using that: https://docs.xlwings.org/en/stable/api.html

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