Python Pandas – how to write a string to a spcific cell via index without using the dataframe

Question:

I am trying to use pandas to write a value to a specific cell via index (1,1), in an xlsx file.
Lets say I currently have an xlsx file:

A    B    C
1    2    3

How can I update 2 to another value without using the whole dataframe please?
For the pupose of what I working on, I would like to specify the value as a string via index (1,1).

Asked By: speedyrazor

||

Answers:

You can just write to the cell using the openpxyl package:

Code:

import openpyxl

# Load the workbook
workbook = openpyxl.load_workbook("example.xlsx")

# Select the sheet you want to modify
worksheet = workbook["Sheet1"]

# Write to a specific cell
worksheet["B2"] = "Another Value"

workbook.save("example.xlsx")
Answered By: ScottC
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.