【发布时间】:2021-10-27 14:35:27
【问题描述】:
我有一个包含 NaN 的分类列“WALLSMATERIAL_MODE”,我想使用以下组 ['NAME_EDUCATION_TYPE', 'AGE_GROUP'] 的模式进行估算:
NAME_EDUCATION_TYPE AGE_GROUP WALLSMATERIAL_MODE
20 Secondary / secondary special 45-60 Stone, brick
21 Secondary / secondary special 21-45 NaN
22 Secondary / secondary special 21-45 Panel
23 Secondary / secondary special 60-70 Mixed
24 Secondary / secondary special 21-45 Panel
25 Secondary / secondary special 45-60 Stone, brick
26 Secondary / secondary special 45-60 Wooden
27 Secondary / secondary special 21-45 NaN
28 Higher education 21-45 NaN
29 Higher education 21-45 Panel
重现性代码
df = pd.DataFrame({'NAME_EDUCATION_TYPE': {20: 'Secondary / secondary special',
21: 'Secondary / secondary special',
22: 'Secondary / secondary special',
23: 'Secondary / secondary special',
24: 'Secondary / secondary special',
25: 'Secondary / secondary special',
26: 'Secondary / secondary special',
27: 'Secondary / secondary special',
28: 'Higher education',
29: 'Higher education'},
'AGE_GROUP': {20: '45-60',
21: '21-45',
22: '21-45',
23: '60-70',
24: '21-45',
25: '45-60',
26: '45-60',
27: '21-45',
28: '21-45',
29: '21-45'},
'WALLSMATERIAL_MODE': {20: 'Stone, brick',
21: np.nan,
22: 'Panel',
23: 'Mixed',
24: 'Panel',
25: 'Stone, brick',
26: 'Wooden',
27: np.nan,
28: np.nan,
29: 'Panel'}})
我尝试从 post 调整以下函数,该函数适用于中值插补并处理为 NaN 的组中值
在:
def mode(s):
if pd.isnull(s.mode()):
return df['WALLSMATERIAL_MODE'].mode()
return s.mode()
df['WALLSMATERIAL_MODE'] = df['WALLSMATERIAL_MODE'].groupby([df['NAME_EDUCATION_TYPE'], df['AGE_GROUP']], dropna=False).apply(lambda x: x.fillna(mode(x)))
OUT:调用 pd.isnull 时出现以下错误
The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
我不明白,我尝试将 pd.isnull 应用于所有组模式,但它不会引发此错误。请参阅下面的组模式
在:
df['WALLSMATERIAL_MODE'].groupby([df['NAME_EDUCATION_TYPE'], df['AGE_GROUP']]).agg(pd.Series.mode).to_dict()
输出:
{('Higher education', '60-70'): nan,
('Higher education', '45-60'): nan,
('Higher education', '21-45'): 'Panel',
('Higher education', '0-21'): nan,
('Secondary / secondary special', '60-70'): 'Mixed',
('Secondary / secondary special', '45-60'): 'Stone, brick',
('Secondary / secondary special', '21-45'): 'Panel',
('Secondary / secondary special', '0-21'): nan}
如果有人能说出错误在哪里,或者是否有有效的方法来按组估算此列,我将不胜感激!
【问题讨论】:
-
使用
median(提供的链接)和使用mode的帖子之间的主要区别在于mode返回Series而median返回单个值。因此,执行if ...s.mode()返回将if与系列一起使用的公共值错误。每次使用mode时,您的函数中都会有一个快速修复,使用...mode().iloc[0] -
感谢 Ben,我尝试使用
pd.isnull(s.mode().iloc[0]),但收到以下错误IndexError: single positional indexer is out-of-bounds。
标签: pandas pandas-groupby nan categorical-data imputation