当被特定半径内的其他值包围时,在 pandas 数据框中获取不同的值然后为零

Getting a different value then zero in pandas dataframe when surrounded by other values in certain radius

在一项大型数据集研究中,我创建了一个包含零 (0) 和一 (1) 的数据集。但是,当值 0 在所有方向上都被 1 包围时,它应该得到值 2。

我在使用 Python 3.7 的 Spyder 环境中工作。没什么了不起的。我只是想不通代码。

import pandas as pd

df = pd.read_excel (r'D:\AW 1920 VU\Research Project\Nieuwe map\Proberen.xlsx') #just an example excel sheet
print (df) 

df2= df.replace(range(1,20) , 1)
print (df2)''' 


df = 
[{0 0   0   0   0   0   0   0   0   0   0   0   0}
{0  0   0   0   0   0   0   0   0   0   0   0   0}
{0  0   0   0   0   0   0   0   0   0   0   0   0}
{0  0   0   0   0   1   0   0   0   0   0   0   0}
{0  0   0   1   11  2   1   1   0    0  0   0   0}
{0  0   0   7   13  1   0   0   0   0   0   0   0}
{0  0   0   2   2   7   0   2   1   0   0   0   0}
{0  0   0   3   5   8   8   2   1   0   0   0   0}
{0  0   0   1   6   7   0   0   1   1   0   0   0}
{0  0   0   1   1   0   0   0   2   0   0   0   0}
{0  0   0   1   1   1   1   0   3   4   0   0   0}
{0  0   0   0   0   1   1   1   2   0   0   0   0}
{0  0   0   0   0   0   1   1   1   0   0   0   0}
{0  0   0   0   0   0   0   0   0   0   0   0   0}
{0  0   0   0   0   0   0   0   0   0   0   0   0}
{0  0   0   0   0   0   0   0   0   0   0   0   0}]

df2=
[{0 0   0   0   0   0   0   0   0   0   0   0   0}
{0  0   0   0   0   0   0   0   0   0   0   0   0}
{0  0   0   0   0   0   0   0   0   0   0   0   0}
{0  0   0   0   0   1   0   0   0   0   0   0   0}
{0  0   0   1   1   1   1   1   0   0   0   0   0}
{0  0   0   1   1   1   0   0   0   0   0   0   0}
{0  0   0   1   1   1   0   1   1   0   0   0   0}
{0  0   0   1   1   1   1   1   1   0   0   0   0}
{0  0   0   1   1   1   0   0   1   1   0   0   0}
{0  0   0   1   1   0   0   0   1   0   0   0   0}
{0  0   0   1   1   1   1   0   1   1   0   0   0}
{0  0   0   0   0   1   1   1   1   0   0   0   0}
{0  0   0   0   0   0   1   1   1   0   0   0   0}
{0  0   0   0   0   0   0   0   0   0   0   0   0}
{0  0   0   0   0   0   0   0   0   0   0   0   0}
{0  0   0   0   0   0   0   0   0   0   0   0   0}]

到目前为止一切正常。但是正如您所看到的,有一个值为 0 的点,周围是 1。我怎样才能 lock/buffer/highlight 那个区域并给它一个 "special value"(2)。所以结果会是这样的:

df3=
[{0 0   0   0   0   0   0   0   0   0   0   0   0}
{0  0   0   0   0   0   0   0   0   0   0   0   0}
{0  0   0   0   0   0   0   0   0   0   0   0   0}
{0  0   0   0   0   1   0   0   0   0   0   0   0}
{0  0   0   1   1   1   1   1   0   0   0   0   0}
{0  0   0   1   1   1   0   0   0   0   0   0   0}
{0  0   0   1   1   1   0   1   1   0   0   0   0}
{0  0   0   1   1   1   1   1   1   0   0   0   0}
{0  0   0   1   1   1   2   2   1   1   0   0   0}
{0  0   0   1   1   2   2   2   1   0   0   0   0}
{0  0   0   1   1   1   1   2   1   1   0   0   0}
{0  0   0   0   0   1   1   1   1   0   0   0   0}
{0  0   0   0   0   0   1   1   1   0   0   0   0}
{0  0   0   0   0   0   0   0   0   0   0   0   0}
{0  0   0   0   0   0   0   0   0   0   0   0   0}
{0  0   0   0   0   0   0   0   0   0   0   0   0}]

希望 table 是可读的。期待回复。

使用代码:

import pandas as pd
import numpy as np 
from scipy import ndimage
#%%
df = np.array ([
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 1,11, 2, 1, 1, 0, 0, 0, 0, 0],
    [0, 0, 0, 7,13, 1, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 2, 2, 7, 0, 2, 1, 0, 0, 0, 0],
    [0, 0, 0, 3, 5, 8, 8, 2, 1, 0, 0, 0, 0],
    [0, 0, 0, 1, 6, 7, 0, 0, 1, 1, 0, 0, 0],
    [0, 0, 0, 1, 1, 0, 0, 0, 2, 0, 0, 0, 0],
    [0, 0, 0, 1, 1, 1, 1, 0, 3, 4, 0, 0, 0],
    [0, 0, 0, 0, 0, 1, 1, 1, 2, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
df2 = np.where(df>=1, 2, df)
df3 = np.where(df2<1, 1, df2)
df4 = np.where(df3==2, 0, df3)

labeled_array, num_features = ndimage.label(df4, np.ones((3,3)))
labeled_array, num_features