代码之家  ›  专栏  ›  技术社区  ›  Laura

python中矩阵中While循环中的增量和减量

  •  1
  • Laura  · 技术社区  · 1 年前

    我创建了一个熊猫数据帧类型的15 x 15矩阵。我想按照以下逻辑对某些单元格进行更改:

    • 矩阵的对角线设置为0
    • 在每一行中,如果0出现在任何位置,则下一列/前五列中的值应更新为1。(df.iloc[i][j]=0->df.iloc[i][j+5]=1或df.iloc[i][j-5]=1

    输入(每个单元格为0.9):

    row, col = 15, 16
    a = pd.DataFrame.from_records([[0.9]*col]*row)
    a = a.loc[ : , a.columns != 0]
    

    enter image description here

    预期输出: enter image description here

    以下是我当前的脚本(它一直在无休止地运行):

    row, col = 15, 16
    a = pd.DataFrame.from_records([[0.9]*col]*row)
    a = a.loc[ : , a.columns != 0]
    column3 = list(a)
    for i, j in a.iterrows():
        for k in column3:
            if k == i + 1:
                a.iloc[i][k] = 0
    
    
    for i, j in a.iterrows():
        for k in column3:
            step = +5 if k <=5 else -5
            while k <= len(a.index):
                if a.iloc[i][k] == 0:
                    k += step
                    a.iloc[i][k] = 1
    
    pd.set_option('display.max_rows', None)
    pd.set_option('display.max_columns', None)
    a 
    

    非常感谢您的帮助!

    1 回复  |  直到 1 年前
        1
  •  2
  •   mozway    1 年前

    我会用 在这里,用 roll :

    # distance to 0
    N = 5
    
    # replace diagonal with 0
    np.fill_diagonal(a.values, 0)
    
    # build mask
    m = (a==0).to_numpy()
    
    # apply mask iteratively
    for i in range(1, a.shape[1]//N):
        a[np.roll(m, i*N, axis=1)] = 1
    

    使用熊猫的变体 shift :

    N = 5
    
    np.fill_diagonal(a.values, 0)
    m = (a==0)
    
    for i in range(1, a.shape[1]//N):
        a[m.shift(i*N, axis=1, fill_value=False)] = 1
        a[m.shift(-i*N, axis=1, fill_value=False)] = 1
    

    输出

         1    2    3    4    5    6    7    8    9    10   11   12   13   14   15
    0   0.0  0.9  0.9  0.9  0.9  1.0  0.9  0.9  0.9  0.9  1.0  0.9  0.9  0.9  0.9
    1   0.9  0.0  0.9  0.9  0.9  0.9  1.0  0.9  0.9  0.9  0.9  1.0  0.9  0.9  0.9
    2   0.9  0.9  0.0  0.9  0.9  0.9  0.9  1.0  0.9  0.9  0.9  0.9  1.0  0.9  0.9
    3   0.9  0.9  0.9  0.0  0.9  0.9  0.9  0.9  1.0  0.9  0.9  0.9  0.9  1.0  0.9
    4   0.9  0.9  0.9  0.9  0.0  0.9  0.9  0.9  0.9  1.0  0.9  0.9  0.9  0.9  1.0
    5   1.0  0.9  0.9  0.9  0.9  0.0  0.9  0.9  0.9  0.9  1.0  0.9  0.9  0.9  0.9
    6   0.9  1.0  0.9  0.9  0.9  0.9  0.0  0.9  0.9  0.9  0.9  1.0  0.9  0.9  0.9
    7   0.9  0.9  1.0  0.9  0.9  0.9  0.9  0.0  0.9  0.9  0.9  0.9  1.0  0.9  0.9
    8   0.9  0.9  0.9  1.0  0.9  0.9  0.9  0.9  0.0  0.9  0.9  0.9  0.9  1.0  0.9
    9   0.9  0.9  0.9  0.9  1.0  0.9  0.9  0.9  0.9  0.0  0.9  0.9  0.9  0.9  1.0
    10  1.0  0.9  0.9  0.9  0.9  1.0  0.9  0.9  0.9  0.9  0.0  0.9  0.9  0.9  0.9
    11  0.9  1.0  0.9  0.9  0.9  0.9  1.0  0.9  0.9  0.9  0.9  0.0  0.9  0.9  0.9
    12  0.9  0.9  1.0  0.9  0.9  0.9  0.9  1.0  0.9  0.9  0.9  0.9  0.0  0.9  0.9
    13  0.9  0.9  0.9  1.0  0.9  0.9  0.9  0.9  1.0  0.9  0.9  0.9  0.9  0.0  0.9
    14  0.9  0.9  0.9  0.9  1.0  0.9  0.9  0.9  0.9  1.0  0.9  0.9  0.9  0.9  0.0
    

    输出为图像以提高清晰度:

    enter image description here