Fillna 使用 groupby 和模式不起作用
我在 Stackoverflow 和其他网站上找到了几个答案。
但是,我不断遇到无法解决的错误。
- 如果我使用它,它工作正常,但这只是列模式。它没有分组。
df['installer'] = df['installer'].fillna(df['installer'].value_counts().idxmax())
- 如果我尝试使用此语法进行分组:
df['installer'] = df.groupby(['region', 'basin'], sort=False)['installer'].apply(
lambda x: x.fillna(x.mode()[0]))
我收到此错误:
KeyError: 0
- 如果我尝试使用此稍微不同的语法进行分组:
df['installer'] = df.groupby(['region', 'basin'], sort=False)['installer'].apply(
lambda x: x.fillna(x.mode().iloc[0]))
我收到此错误:
IndexError: single positional indexer is out-of-bounds
删除 sort=False 不会改变任何内容。
额外信息:
df['installer'] = df['installer'].astype(str).str.lower()
print(df['installer'].isnull().sum(), '\n') # zero nulls at this point
df.loc[df['installer'] == '0', 'installer'] = np.nan
df.loc[df['installer'] == 'nan', 'installer'] = np.nan
df.loc[df['installer'] == '-', 'installer'] = np.nan
# df['installer'] = df['installer'].fillna(df['installer'].value_counts().idxmax())
print(df['installer'].isnull().sum(), '\n') # 4435 null values here
print(df3['installer'].value_counts().nlargest(25), '\n')
df['installer'] = df.groupby(['region', 'basin'], sort=False)['installer'].apply(
lambda x: x.fillna(x.mode().iloc[0]))
# df['installer'] = df.groupby(['region', 'district_code', 'lga'])['installer'].fillna(df['installer'].value_counts().idxmax())
print(df['installer'].isnull().sum(), '\n')
print(df['installer'].value_counts().nlargest(25), '\n')
I have found several answers to this both here on Stackoverflow and other sites.
However, I keep running into errors I can't resolve.
- If I fillna using this, it works fine, but this is just the column mode. It is not grouped.
df['installer'] = df['installer'].fillna(df['installer'].value_counts().idxmax())
- If I try grouping with this syntax:
df['installer'] = df.groupby(['region', 'basin'], sort=False)['installer'].apply(
lambda x: x.fillna(x.mode()[0]))
I get this error:
KeyError: 0
- If I try grouping with this, slightly different syntax:
df['installer'] = df.groupby(['region', 'basin'], sort=False)['installer'].apply(
lambda x: x.fillna(x.mode().iloc[0]))
I get this error:
IndexError: single positional indexer is out-of-bounds
Removing sort=False changes nothing.
Extra info:
df['installer'] = df['installer'].astype(str).str.lower()
print(df['installer'].isnull().sum(), '\n') # zero nulls at this point
df.loc[df['installer'] == '0', 'installer'] = np.nan
df.loc[df['installer'] == 'nan', 'installer'] = np.nan
df.loc[df['installer'] == '-', 'installer'] = np.nan
# df['installer'] = df['installer'].fillna(df['installer'].value_counts().idxmax())
print(df['installer'].isnull().sum(), '\n') # 4435 null values here
print(df3['installer'].value_counts().nlargest(25), '\n')
df['installer'] = df.groupby(['region', 'basin'], sort=False)['installer'].apply(
lambda x: x.fillna(x.mode().iloc[0]))
# df['installer'] = df.groupby(['region', 'district_code', 'lga'])['installer'].fillna(df['installer'].value_counts().idxmax())
print(df['installer'].isnull().sum(), '\n')
print(df['installer'].value_counts().nlargest(25), '\n')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
转换
:Use
transform
: