Python 当数据帧中的值被某些半径内的其他值包围时,获取一个不同的值,然后为零

Python 当数据帧中的值被某些半径内的其他值包围时,获取一个不同的值,然后为零,python,pandas,zero,Python,Pandas,Zero,在一项关于大数据集的研究中,我创建了一个包含0和1的数据集。但是,当值0在所有方向上都被1包围时,其值应为2 我使用Python3.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.rep

在一项关于大数据集的研究中,我创建了一个包含0和1的数据集。但是,当值0在所有方向上都被1包围时,其值应为2

我使用Python3.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。如何锁定/缓冲/突出显示该区域并为其指定“特殊值”(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
将熊猫作为pd导入
将numpy作为np导入
从scipy导入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.其中(df>=1,2,df)
df3=np。其中(df2使用的代码:

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
将熊猫作为pd导入
将numpy作为np导入
从scipy导入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.其中(df>=1,2,df)

df3=np.where(DF2C)你能试试这个吗?这是一个类似问题的解决方案:非常感谢Arran。你的链接确实有解决方案。你能试试吗?这是一个类似问题的解决方案:非常感谢Arran。你的链接确实有解决方案。