【发布时间】:2019-08-12 09:46:50
【问题描述】:
我已经从一个矩阵生成了一个数据框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
我想通过对df 的相邻单元格3x3 求和来生成一个新的数据帧df1。
### 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
【问题讨论】:
-
澄清一下:每个总和,你跳过一行一列?
-
@MarcusLim 在我的情况下,我有一个 103 x 159 的矩阵,我必须找到一个解决方案来聚合这些值。在这种情况下,每个总和我都会跳过两行和两列
-
添加了标签numpy,我觉得应该有一些方法。
-
看起来像一个简单的卷积
标签: python pandas numpy group-by