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?
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.
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.
replace() function is used to replace values given in to_replace with value. The values of the Series are replaced with other values dynamically.
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.
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")
.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With