pandas 重命名 csv 文件中的列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46066685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:24:10  来源:igfitidea点击:

Rename the column inside csv file

pythonpandas

提问by Fiqri Mfbw

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.

任何人都可以帮我检查我的重命名命令有什么问题。它对 csv 文件没有任何改变。我在下面重命名标题中尝试过的代码。

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

原始 CSV.file

Click Here

点击这里

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

现在 KeyError: 'Custom field (Implemented Date)' 不再执行。就在我运行这段代码之后。

The output will display as below.

输出将显示如下。

enter image description here

在此处输入图片说明

回答by Vaishali

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)'}))

回答by R.A.Munna

you can call rename function with external parameter inplace=True

您可以使用外部参数调用重命名函数 inplace=True

df.rename(columns={'Custom field (Implemented Date)': 'Custom field (Verified Date)'}, inplace=True)

For more see pandas.DataFrame.renameand Renaming columns in pandas

有关更多信息,请参阅pandas.DataFrame.rename重命名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.

毕竟,我得到了上面描述的文件中的输出,没有任何错误。

回答by Bora Savkar

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)