AMayer
AMayer

Reputation: 425

Add a title to a pandas.core.series.Series

So I have this code to read an excel file:

import pandas as pd

DataFrame = pd.read_excel("File.xlsx", sheetname=0)
DataFrame.groupby(["X", "Y"]).size()

res = DataFrame.groupby(["X", "Y"]).size()
print res

This code:

res = DataFrame.groupby(["X", "Y"]).size()

Return how many time the items from X appears in the file for example, if I have the following example:

X     Y 
abc   test
abc   test
a     test

I get

X    Y
abc test  2
a   test  1

How can I add a title to the 3rd column, so i can sort it, for example I want:

X    Y    Z
abc test  2
a   test  1

and how can I write it to an excel file where each column from the output would be a column in excel?

Upvotes: 0

Views: 250

Answers (1)

IanS
IanS

Reputation: 16251

Try either:

res.rename('Z').sort_values().to_excel(...)

Or:

res.rename('Z').to_frame().sort_values(by='Z').to_excel(...)

Upvotes: 2

Related Questions