pandas 在熊猫中重命名系列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16350864/
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-13 20:48:13  来源:igfitidea点击:

Renaming series in pandas

pythonpandas

提问by user1867185

I am working with series, and I was wondering how I can rename the series when writing to a file. For example, my output csv consists of the following:

我正在使用系列,我想知道如何在写入文件时重命名系列。例如,我的输出 csv 包含以下内容:

Gene_Name,0
A2ML1,15
AAK1,8

I want it to be the following:

我希望它如下:

Gene_Name,Count
A2ML1,15
AAK1,8

Note: I don't want my header to be "Gene_Name,0" but "Gene_Name,Count." How can I accomplish this?

注意:我不希望我的标题是“Gene_Name,0”而是“Gene_Name,Count”。我怎样才能做到这一点?

回答by bdiamante

To make "Count" the name of your series, just set it with your_series.name = "Count"and then call to_csv like this: your_series.to_csv("c:\\output.csv", header=True, index_label="Gene_Name").

要将“计数”作为您系列的名称,只需将其设置为your_series.name = "Count",然后像这样调用 to_csv:your_series.to_csv("c:\\output.csv", header=True, index_label="Gene_Name")

回答by Kamil Sindi

Another way to do it:

另一种方法:

s.to_frame("Count").to_csv("output.csv", header=True, index_label="Gene_Name")

or

或者

s.reset_index(name="Count").to_csv("output.csv", header=True, index_label="Gene_Name")