按分组行和按列迭代填充 Na



图像:https://i.stack.imgur.com/nljvJ.png它不允许我上传,因为我还没有信誉点,但它是数据帧的截图

我有一个pandas数据框架,它有很多位置(行(,每个位置都有相同的年份(1990-2020(和列,然后有很多属性列,每个列都有数据。我想找到每个位置的每个专栏的所有年份的平均值。

这就是我正在尝试的,但它不起作用。

location_names_list=[‘中欧和波罗的海国家’,‘加勒比海小国’…

worldbank是一个pandas数据框架。

world_bank = wb.download(indicator=indicators_list, country=countries, start=1990, end=2019)
for location in location_names_list: 
for column in world_bank:
mean_variable = world_bank.loc[location, column].mean()
world_bank.loc[location, column].fillna(mean_variable, inplace=True)

请查看Paul在最佳发布实践评论部分的说明。

不管怎样,我知道你只想要一个使用分组平均值的fillna。是吗?

如果是这样的话,我相信groupby+transform+lambda可以在一行中帮助您。

例如:

df['Central Europe and the Baltics'] = df.groupby('country')['Central Europe and the Baltics'].transform(lambda x: x.fillna(x.mean()))

因此,要遍历所有列,可以执行以下操作:

for col in df.columns:
df[col] = df.groupby('country')[col].transform(lambda x: x.fillna(x.mean()))

最新更新