在对大数据集的研究中,我创建了一个零(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的斑点,被一个点包围。如何锁定/缓冲/突出显示该区域并为其指定“特殊值”(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}]
希望该表可读。期待得到答复。
最佳答案
使用的代码:
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