【问题标题】:Python: how to reshape a dataframe by summing neighbour cells?Python:如何通过对相邻单元求和来重塑数据框?
【发布时间】: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


【解决方案1】:

你可以用df.shift做到这一点

axes = (0, 1)
shifts = -1, 1
intermediate_sum = (
    df
    + sum(df.shift(shift, axis=axis) for shift, axis in product(shifts, axes))
)
result = (
    intermediate_sum.dropna(how="all", axis=0)
    .dropna(how="all", axis=1)
    .iloc[::2, ::2]
)
result
  1   3
1 23.0    22.0
3 35.0    33.0
5 18.0    18.0

【讨论】:

  • 我得到以下信息:AxisError: axis 1 is out of bounds for array of dimension 1 当我尝试计算 intermediate_sum
  • 结果与预期不同?
  • @RafaelC 随机数生成,无需设置种子。请参阅 OP 的顶部。
【解决方案2】:

您可以在 scipy 中使用带有convolve2d 函数的卷积来实现:

M = np.random.randint(10, size=(7, 5))
print(M)

[[9 2 4 5 8]
 [4 0 3 4 9]
 [9 4 6 3 0]
 [4 6 9 9 5]
 [4 3 1 3 9]
 [9 2 9 0 7]
 [4 3 7 6 1]]


from scipy.signal import convolve2d

r = convolve2d(M, np.ones([3,3]), mode='same')[1::2,1::2]
print(r)

[[41. 42.]
 [46. 45.]
 [42. 43.]]

这里np.ones([3,3]) 生成掩码,3x3 矩阵:

array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])

我使用 [1::2, 1::2] 索引从元素 1 而不是 0 开始,并像在问题中那样跳过所有其他行/列。

另请参阅Getting sum of adjacent elements of a matrix 了解更多信息

【讨论】:

  • 这正是我想要的。一个问题:为什么你有np.ones([3,3])
  • np.ones([3,3]) 生成掩码,1 的 3x3 矩阵
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-11
  • 2020-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多