Используемый код:
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