Reputation: 243
Can anyone please check for me what's wrong with my renaming command. It changes nothing on the csv file. The code that i have tried below renaming header.
df = pandas.read_csv('C:/JIRA Excel File.csv')
df.rename(columns=({'Custom field (Implemented Date)':'Custom field (Verified Date)'}))
df.set_index('Custom field (Verified Date)').to_csv("C:/JIRA Excel File/Done.csv", index=None)
I want column Custom field (Implemented Date) CHANGE to Custom field (verified Date), but the column still doesn't change.
Original CSV.file
Now the KeyError: 'Custom field (Implemented Date)' is not execute anymore. Just after I run this code.
The output will display as below.
Upvotes: 2
Views: 29920
Reputation: 75
You can simply use:
renamed_df=df.rename(columns={'Custom field (Implemented Date)':'Custom field (Verified Date)'})
renamed_df.to_csv("C:/JIRA Excel File/Done.csv", index=None)
Upvotes: 0
Reputation: 1709
you can call rename function with external parameter inplace=True
df.rename(columns={'Custom field (Implemented Date)': 'Custom field (Verified Date)'}, inplace=True)
For more see pandas.DataFrame.rename and Renaming columns in pandas
Update: from your comment and updated question
# considering a sample csv from your description and the df is.
'''
Issue Type Custom field (Verified Date) Custom field (Implemented Date)
0 issue-1 varified-date1 Implemented-Date1
1 issue-2 varified-date2 Implemented-Date2
'''
# first delete the 'Custom field (Verified Date)' column
del df['Custom field (Verified Date)']
'''
Issue Type Custom field (Implemented Date)
0 issue-1 Implemented-Date1
1 issue-2 Implemented-Date2
'''
# rename the column 'Custom field (Implemented Date)' to 'Custom field (Verified Date)'
df.rename(columns={'Custom field (Implemented Date)': 'Custom field (Verified Date)'}, inplace=True)
'''
Issue Type Custom field (Verified Date)
0 issue-1 Implemented-Date1
1 issue-2 Implemented-Date2
'''
df.set_index('Custom field (Verified Date)').to_csv("Done.csv", index=None)
And after all this I get the output in file as you describe above with out any error.
Upvotes: 4
Reputation: 38415
You are not assigning the result of rename back to the dataframe. Change the 2nd line to
df = df.rename(columns=({'Custom field (Implemented Date)':'Custom field (Verified Date)'}))
Upvotes: 4