无法将DataFrame与类型为<class 'pandas.core.groupby.DataFrameGroupBy'>的实例合并

2024-09-22 16:39:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力解决这个错误

ValueError: can not merge DataFrame with instance of type <class 'pandas.core.groupby.DataFrameGroupBy'>

其中,我想合并由创建的两个数据帧,其中aggas

从第一个df i创建的df

^{pr2}$

然后我运行了我想要的数据

resi_flats_nooutliers_bysector['updatedprice_calculated'].
agg([np.mean,np.median,np.max,'count'])

resi_all_nooutliers_bysector['updatedprice_calculated'].
agg([np.mean,np.median,np.max,'count'])

然后我试图合并为

df_resi_nooutliers_bysector = pd.merge(resi_all_nooutliers_bysector, 
                                       resi_flats_nooutliers_bysector,
                                       on=['postcode_sector'],how='left', 
                                       suffixes=('_allprop', '_flats'))

获取标题错误


Tags: 数据df错误npmergemeanmaxagg
1条回答
网友
1楼 · 发布于 2024-09-22 16:39:07

对我来说,这是有效的,将agg输出保存到一个数据帧中,确保索引在原始索引上(列postcode_sectors

df1 = resi_flats_nooutliers_bysector['updatedprice_calculated'].\
agg([np.mean,np.median,np.max,'count'],as_index=False)

df2 = resi_all_nooutliers_bysector['updatedprice_calculated'].\
agg([np.mean,np.median,np.max,'count'],as_index=False)
type (resi_flats_nooutliers_bysector)
df1.head(10)

然后使用join的索引

^{pr2}$

相关问题 更多 >