简体   繁体   中英

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?

Try either:

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

Or:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM