您需要先按照前两列进行分组,然后应用转换以加入结果。
df['Neighborhood ']= df.groupby(['PostalCode ','Borough'])['Neighboudhood'].transform(lambda x: ','.join(x))
df = df.drop_duplicates()
df = df.groupby(['PostalCode','Borough'])['Neighbourhood'].agg(','.join)
这是两行输出的示例:
CR0 Croydon Addington,Addiscombe