Я сгенерировал фрейм данных df
из матрицы.
M=np.random.randint(10, size=(7, 5))
df = pd.DataFrame(M)
df
0 1 2 3 4
0 8 3 2 2 5
1 5 8 1 5 6
2 1 9 1 4 2
3 0 7 7 6 9
4 5 8 7 0 9
5 0 3 9 9 4
6 7 7 8 5 4
Я хотел бы создать новый фрейм данных df1
путем суммирования соседних ячеек 3x3
из df
.
### Aggregate rows 0,1,2 and columns 0,1,2
df1[0][0] = [8+3+2+5+8+1+1+9+1] = 38
### Aggregate rows 0,1,2 and columns 2,3,4
df1[1][0] = [2+2+5+1+5+6+1+4+2] = 28
### Aggregate rows 2,3,4 and columns 0,1,2
df1[1][0] = [1+9+1+0+7+7+5+8+7] = 45
### Aggregate rows 2,3,4 and columns 2,3,4
df1[1][1] = [1+4+2+7+6+9+7+0+9] = 45
### Aggregate rows 4,5,6 and columns 0,1,2
df1[2][0] = [5+8+7+0+3+9+7+7+8] = 55
### Aggregate rows 4,5,6 and columns 2,3,4
df1[2][1] = [7+0+9+9+9+4+8+5+4] = 55
df1
0 1
0 38 28
1 45 45
2 55 55