Skip to content Skip to sidebar Skip to footer

Grouping On Identical Column Names In Pandas

time A1 A1 A2 A2 A2 A3 A3 2017-01 a1 a2 b1 b2 c ..... 2017-02 a3 a4 b3 b4 c 2017-03 a5 a6 b5 b6 c ...

Solution 1:

Use groupby with level=0 and axis=1.

df.groupby(level=0, axis=1).mean()

np.random.seed(0)
df = pd.DataFrame(np.random.choice(10, (3, 5)), columns=list('AAABB'))

df

   A  A  A  B  B
0  5  0  3  3  7
1  9  3  5  2  4
2  7  6  8  8  1

df.groupby(level=0, axis=1).mean()

          A    B
0  2.666667  5.0
1  5.666667  3.0
2  7.000000  4.5

Post a Comment for "Grouping On Identical Column Names In Pandas"