user1867185
user1867185

Reputation: 407

Renaming series in pandas

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:

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?

Upvotes: 8

Views: 7922

Answers (2)

Kamil Sindi
Kamil Sindi

Reputation: 22832

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")

Upvotes: 2

bdiamante
bdiamante

Reputation: 17570

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").

Upvotes: 11

Related Questions