groupby
也可以工作axis=1
,并可以接受一系列的组标签.如果你的列是方便的范围,如你的例子,它是微不足道的:
>>> df = pd.DataFrame((np.random.randn(6*6)).reshape(6,6)) >>> df 0 1 2 3 4 5 0 1.705550 -0.757193 -0.636333 2.097570 -1.064751 0.450812 1 0.575623 -0.385987 0.105516 0.820795 -0.464069 0.728609 2 0.776840 -0.173348 0.878534 0.995937 0.094515 0.098853 3 0.326854 1.297625 2.232534 1.004719 -0.440271 1.548430 4 0.483211 -1.182175 -0.012520 -1.766317 -0.895284 -0.695300 5 0.523011 -1.653557 1.022042 1.201774 -1.118465 1.400537 >>> df.groupby(df.columns//2, axis=1).mean() 0 1 2 0 0.474179 0.730618 -0.306970 1 0.094818 0.463155 0.132270 2 0.301746 0.937235 0.096684 3 0.812239 1.618627 0.554080 4 -0.349482 -0.889419 -0.795292 5 -0.565273 1.111908 0.141036
(这有效,因为df.columns//2
给Int64Index([0, 0, 1, 1, 2, 2], dtype='int64')
.)
即使我们不是那么幸运,我们仍然可以自己建立适当的团体:
>>> df.groupby(np.arange(df.columns.size)//2, axis=1).mean() 0 1 2 0 0.474179 0.730618 -0.306970 1 0.094818 0.463155 0.132270 2 0.301746 0.937235 0.096684 3 0.812239 1.618627 0.554080 4 -0.349482 -0.889419 -0.795292 5 -0.565273 1.111908 0.141036