我有一个名为 shoes 的数据框Brand CommentUgg NaNPrada NaNClarks NaNUgg NaNClark NaNPrada Made from horse leatherPrada Made from pig leatherPrada NaNUgg Made from Australian cow leather...另一个数据框 df_mode 是通过将鞋子数据框中每个鞋子品牌的评论模式取为非空值而获得的Brand CommentUgg Made from sheep Prada Made from pig leatherClarks Made from Cow leather如何使用 df_mode 数据框中显示的相应模式响应为鞋子数据框中的每个鞋子品牌分配缺失值。这基本上就是我想要实现的Brand CommentUgg Made from sheepPrada Made from pig leatherClarks Made from Cow leatherUgg Made from sheepClark Made from Cow leatherPrada Made from horse leatherPrada Made from pig leatherPrada Made from pig leatherUgg Made from Australian cow leather
3 回答
蛊毒传说
TA贡献1895条经验 获得超3个赞
使用loc
和map
:
shoes.loc[shoes.Comment.isna(), 'Comment'] = shoes.Brand.map(df_mode.set_index('Brand')['Comment'])
慕神8447489
TA贡献1780条经验 获得超1个赞
使用 np.where
shoes['Comment']=np.where(shoes['Comment'].isnull(),shoes['Brand'].map(dict(zip(df_mode['Brand']))),df_mode['Comment'],shoes['Comment'])
30秒到达战场
TA贡献1828条经验 获得超6个赞
您可以groupby
先按品牌列,然后填写缺失值。这是实现:
df['Comment'] = df.groupby(['Brand'], sort=False)['Comment'].apply(lambda x: x.ffill().bfill())
添加回答
举报
0/150
提交
取消