Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 重命名csv文件中的列_Python_Pandas - Fatal编程技术网

Python 重命名csv文件中的列

Python 重命名csv文件中的列,python,pandas,Python,Pandas,谁能帮我检查一下我的重命名命令有什么问题吗。它不会更改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

谁能帮我检查一下我的重命名命令有什么问题吗。它不会更改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)
我想将列自定义字段(实施日期)更改为自定义字段 (已验证日期),但列仍然没有更改

原始CSV.file

现在KeyError:“Custom field(Implemented Date)”不再执行。 在我运行这个代码之后

输出将显示如下


您没有将重命名的结果分配回数据帧。将第二行更改为

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

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

df.rename(columns={'Custom field (Implemented Date)': 'Custom field (Verified Date)'}, inplace=True)
有关更多信息,请参阅和

更新: 根据您的评论和更新的问题

# 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)
在所有这些之后,我得到了上面描述的文件中的输出,没有任何错误

您可以简单地使用:

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)

嗨,我更改了第二行代码,它给出了错误。KeyError:“自定义字段(实施日期)”在执行df=pandas.read_csv('C:/JIRA Excel File.csv')后,您是否可以发布通过df.columns获得的信息?“关键错误”表示您尝试更改的列名不是“自定义字段(实施日期)”是否要更改列名?是的,我希望它删除列验证日期并将实施日期重命名为验证日期。因此,输出将显示问题类型和验证日期,我将其重命名为实施日期。请查看更新的答案。希望这会对你有更多的帮助。是的,但即使我用你的代码替换了,实际上我所接受的所有命令也与你提到的链接相同。最后还将删除“自定义字段(实施日期)”。