pd.concat报错TypeError: concat() got an unexpected keyword argument 'join_axes'如何解决
问题原因
join_axes是 pandas 0.25 版本的废弃参数,在 pandas 1.0 及之后的正式版本中已被彻底移除,因此调用pd.concat()时传入该参数会抛出不存在该关键字参数的报错。- 你当前代码的逻辑是从原数据帧拆分出风格字段做独热编码后再拼接回原表,两个待拼接对象的索引天然同源对齐,不需要额外指定对齐规则。
修复方案
直接删除代码中 join_axes 参数即可正常运行,修正后代码如下:
Genre = movie_ratings_selected_df['Genres'] Genre = Genre.str.get_dummies().add_prefix('Genres_') movie_ratings_genres_df = pd.concat( [movie_ratings_selected_df.drop( ['Genres'], axis=1 ), Genre], axis=1 ) movie_ratings_genres_df.head()
如果后续遇到需要实现原 join_axes 指定索引对齐的通用场景,可以提前用 reindex 方法处理待拼接的对象,写法如下:
movie_ratings_genres_df = pd.concat( [ movie_ratings_selected_df.drop(['Genres'], axis=1), # 提前按指定索引重对齐要拼接的对象 Genre.reindex(movie_ratings_selected_df.index) ], axis=1 )
内容的提问来源于stack exchange,提问作者JBelo
相关产品推荐
相关产品推荐

