Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to insert data into a specific cell in csv with python?

I am trying to insert data into a specific cell in csv. My code is as follows.

The existing file.

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Output

The data in cell A1("Custmor") is replaced with new data("Name").

enter image description here

My code is as follows.

import pandas as pd

#The existing CSV file
file_source = r"C:\Users\user\Desktop\Customer.csv"

#Read the existing CSV file
df = pd.read_csv(file_source)

#Insert"Name"into cell A1 to replace "Customer"
df[1][0]="Name"

#Save the file
df.to_csv(file_source, index=False)

And it doesn’t work. Please help me finding the bug.

>Solution :

Customer is column header, you need do

df = df.rename(columns={'Customer': 'Name'})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading