2

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

Click Here

Now the KeyError: 'Custom field (Implemented Date)' is not execute anymore. Just after I run this code.

The output will display as below.

enter image description here

Fiqri Mfbw
  • 243
  • 2
  • 5
  • 12

3 Answers3

4

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)'}))
Vaishali
  • 37,545
  • 5
  • 58
  • 86
  • Hi, I've change the 2nd line code, and it gives error. KeyError: 'Custom field (Implemented Date)' – Fiqri Mfbw Sep 06 '17 at 04:27
  • Can you post what are you getting with df.columns after executing df = pandas.read_csv('C:/JIRA Excel File.csv')? Key error means that the column name that you are trying to change is not 'Custom field (Implemented Date)' – Vaishali Sep 06 '17 at 04:32
4

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.

R.A.Munna
  • 1,699
  • 1
  • 15
  • 29
  • yaa but still even I replaced with your code, and actually all of the command I take also with the same link that you refers. At the end it will delete 'Custom field (Implemented Date)' as well. – Fiqri Mfbw Sep 06 '17 at 05:59
0

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)
user158
  • 12,852
  • 7
  • 62
  • 94