我想合并两只熊猫的资料:
df 1
City | Attraction | X | Z | Y
Somewhere Rainbows 1 2 3
Somewhere Trees 4 4 4
Somewhere Unicornsdf 2
City | Other Column | Also another column
Somewhere Something Something else通常是这样做的:
df2.merge(df1[['City', 'Attraction']], left_on='City', right_on='City. how='left']
City | Other Column | Also another column | Attraction
Somewhere Something Something else Rainbows
Somewhere Something Something else Trees
Somewhere Something Something else Unicorns但是,我想将联接的结果分组为一个逗号分隔的列表(或其他任何内容):
City | Other Column | Also another column | Attraction
Somewhere Something Something else Rainbows, Trees, Unicorns发布于 2020-03-04 15:40:14
groupby()和map
df2['Attaction'] = df2['City'].map(df1.groupby('City').Attraction.agg(', '.join))https://stackoverflow.com/questions/60529616
复制相似问题