Gere
Gere

Reputation: 12697

Aggregate over an index in pandas?

How can I aggregate (sum) over an index which I intend to map to new values? Basically I have a groupby result by two variables where I want to groupby one variable into larger classes. The following code does this operation on s by mapping the first by-variable but seems too complicating:

import pandas as pd

mapping={1:1, 2:1, 3:3}

s=pd.Series([1]*6, index=pd.MultiIndex.from_arrays([[1,1,2,2,3,3],[1,2,1,2,1,2]]))

x=s.reset_index()
x["level_0"]=x.level_0.map(mapping)

result=x.groupby(["level_0", "level_1"])[0].sum()

Is there a way to write this more concisely?

Upvotes: 1

Views: 106

Answers (1)

CT Zhu
CT Zhu

Reputation: 54340

There is a level= option for Series.sum(), I guess you can use that and it will be a quite concise way to do it.

In [69]:

s.index = pd.MultiIndex.from_tuples(map(lambda x: (mapping.get(x[0]), x[1]), s.index.values))
s.sum(level=(0,1))
Out[69]:
1  1    2
   2    2
3  1    1
   2    1
dtype: int64

Upvotes: 3

Related Questions