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



我正在尝试解决此错误;

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

我想合并由aggas 创建的两个数据帧;

首先,我从主 df 创建了一个分组数据的 df;

resi_all_nooutliers_bysector = df_resi_rawdata_nooutliers.groupby(['postcode_sector'])
resi_flats_nooutliers_bysector = df_resi_rawdata_nooutliers.loc[df_resi_rawdata_nooutliers['propertytype']=='F'].groupby(['postcode_sector'])

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

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'))

在标题中获取错误

对我来说,这很有效,将 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)

然后使用索引进行联接

merge_test = df2.merge(df1, left_index=True, right_index=True,suffixes=
('_allprop', '_flats'))
merge_test.head(10)`

相关内容

  • 没有找到相关文章

最新更新