求助:pandas 在使用两次merge后无法进行apply

在dataframe进行两次merge后,利用apply使用函数,程序一直运行,无法输出

t = user_shop[['user_id','shop_id','longitude','latitude']]
t.rename(columns={'longitude':'user_longitude','latitude':'user_latitude'}, inplace=True)
t1 = shop_info[['shop_id','mall_id','longitude','latitude']]
t1.rename(columns={'longitude':'shop_longitude','latitude':'shop_latitude'}, inplace=True)
t2 = pd.merge(t,t1[['shop_id','mall_id']],on=['shop_id'],how='left')
t1.rename(columns={'shop_id':'shop_id_same_mall'}, inplace=True)
t3 = pd.merge(t2,t1,on=['mall_id'],how='left')

def cal_dis(a):
    return a['user_id']

t3['distance'] = t3.apply(cal_dis, axis=1)
print (t3.loc[1:10])

可是在分别使用t,t1和merge过一次的t2代替t3时,都会有结果输出,只有进行两次merge的t3始终得不到输出,请问问题到底出在哪里了呢?

阅读 2.9k
1 个回答
你的代码
t3['distance'] = t3.apply(cal_dis, axis=1)
和
t3['distance'] = t3['user_id']
没有任何区别
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题