如何为 pandas 中的每个指标实现这样的每个国家/地区的插补?
我想估算每组的缺失值
- no-A-state应该得到
np.min
每个指标KPI - no-ISO-state应该得到
np.mean
每个指标KPI 对于缺失值的状态,我想用每个
indicatorKPI
平均值来估算。在这里,这意味着估算塞尔维亚的缺失值mydf = pd.DataFrame({'Country':['no-A-state','no-ISO-state','germany','serbia','austria','germany','serbia','austria ',], 'indicatorKPI':[np.nan,np.nan,'SP.DYN.LE00.IN','NY.GDP.MKTP.CD','NY.GDP.MKTP.CD', 'SP. DYN.LE00.IN','NY.GDP.MKTP.CD', 'SP.DYN.LE00.IN'], '值':[np.nan,np.nan,0.9,np.nan,0.7, 0.2 , 0.3, 0.6]})
编辑
所需的输出应类似于
mydf = pd.DataFrame({'Country':['no-A-state','no-ISO-state', 'no-A-state','no-ISO-state',
'germany','serbia','serbia', 'austria',
'germany','serbia', 'austria',],
'indicatorKPI':['SP.DYN.LE00.IN','NY.GDP.MKTP.CD', 'SP.DYN.LE00.IN',
'SP.DYN.LE00.IN','NY.GDP.MKTP.CD','SP.DYN.LE00.IN','NY.GDP.MKTP.CD','NY.GDP.MKTP.CD', 'SP.DYN.LE00.IN','NY.GDP.MKTP.CD', 'SP.DYN.LE00.IN'],
'value':['MIN of all for this indicator', 'MEAN of all for this indicator','MIN of all for this indicator','MEAN of all for this indicator', 0.9,'MEAN of all for SP.DYN.LE00.IN indicator',0.7, 'MEAN of all for NY.GDP.MKTP.CD indicator',0.2, 0.3, 0.6]
})