0

我有一个 geopandas 数据框,其中有一列中有 NaN。我想用其邻居的平均值来估算 NaN。我编写了以下示例,如果有人可以帮助我完成最后的步骤,我将不胜感激。谢谢。

# Load libraries
import numpy as np
import pandas as pd
import geopandas as gpd
from libpysal.weights.contiguity import Queen

# Make data
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
africa = world[world['continent'] == 'Africa']
africa.reset_index(inplace=True, drop=True)
africa.loc[[2,8,15,22,30,35,39,43],'pop_est'] = np.nan # Make NaN value for pop_est
africa

# Generate weight
w = Queen.from_dataframe(africa)
w.neighbors[2] # Check neighbors of index 2

例如,index 2人口估计有一个缺失值,它的邻居是[0, 35, 36, 48, 49, 50, 27, 28, 31]。我想使用人口估计的平均值[0, 35, 36, 48, 49, 50, 27, 28, 31]来替换 NaN。谢谢。

4

1 回答 1

0

我终于想出了怎么做。

于 2021-07-27T02:29:02.043 回答