Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming series in pandas

Tags:

python

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?

like image 534
user1867185 Avatar asked May 03 '13 02:05

user1867185


People also ask

How do you rename a series?

Right-click the chart with the data series you want to rename, and click Select Data. In the Select Data Source dialog box, under Legend Entries (Series), select the data series, and click Edit.

Is it possible to assign a name to the index of a pandas series?

The index can be label names (object data) or it can be values. By default, it will assign an index value from 0 - n-1 (n is the length of series values). And it has the ability to define index values.

How do I change the value of series in pandas?

replace() function is used to replace values given in to_replace with value. The values of the Series are replaced with other values dynamically.

How does rename work in pandas?

The output of Pandas renameBy default, the rename method will output a new Python dataframe, with new column names or row labels. As noted above, this means that by default, rename will leave the original dataframe unchanged. If you set inplace = True , rename won't produce any new output.


2 Answers

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

like image 175
bdiamante Avatar answered Sep 17 '22 17:09

bdiamante


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")
like image 43
Kamil Sindi Avatar answered Sep 20 '22 17:09

Kamil Sindi