I am trying to insert data into a specific cell in csv. My code is as follows.
The existing file.
Output
The data in cell A1(“Custmor”) is replaced with new data(“Name”).
My code is as follows.
JavaScript
x
14
14
1
import pandas as pd
2
3
#The existing CSV file
4
file_source = r"C:UsersuserDesktopCustomer.csv"
5
6
#Read the existing CSV file
7
df = pd.read_csv(file_source)
8
9
#Insert"Name"into cell A1 to replace "Customer"
10
df[1][0]="Name"
11
12
#Save the file
13
df.to_csv(file_source, index=False)
14
And it doesn’t work. Please help me finding the bug.
Advertisement
Answer
Customer
is column header, you need do
JavaScript
1
2
1
df = df.rename(columns={'Customer': 'Name'})
2