gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:17:40 32 4
gpt4 key购买 nike

在一项大数据集的研究中,我创建了一个包含零 (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。我如何锁定/缓冲/突出显示该区域并赋予它“特殊值”(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

关于python - 当被特定半径内的其他值包围时,在 pandas 数据框中获取不同的值然后为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58354960/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com