pandas Column selection of a group


Example

When you do a groupby you can select either a single column or a list of columns:

In [11]: df = pd.DataFrame([[1, 1, 2], [1, 2, 3], [2, 3, 4]], columns=["A", "B", "C"])

In [12]: df
Out[12]:
   A  B  C
0  1  1  2
1  1  2  3
2  2  3  4

In [13]: g = df.groupby("A")

In [14]: g["B"].mean()           # just column B
Out[14]:
A
1    1.5
2    3.0
Name: B, dtype: float64

In [15]: g[["B", "C"]].mean()    # columns B and C
Out[15]:
     B    C
A
1  1.5  2.5
2  3.0  4.0

You can also use agg to specify columns and aggregation to perform:

In [16]: g.agg({'B': 'mean', 'C': 'count'})
Out[16]:
   C    B
A        
1  2  1.5
2  1  3.0