如何合并对特定列具有相同值的行?
How to combine rows that have the same value for specific columns?
我已经合并了两个数据框,我想合并位置列具有重复值的行,但合并性能列的值,同时保持纬度和经度值。我该怎么做?
按感兴趣的列(location
、latitude
、longitude
)分组并求和:
df.groupby(['location', 'latitude', 'longitude']).sum()
如果您不想在索引中保留分组列,请使用 .reset_index()
跟进:
df.groupby(['location', 'latitude', 'longitude']).sum().reset_index()
这取决于您想对性能列做什么。你说你想合并这些值?我假设您的意思是“总结”这些值。
如果你有这样的 df:
您需要按位置、纬度和经度分组。总结表现。
df.groupby(['location', 'lat', 'lng']).performances.sum().reset_index()
我已经合并了两个数据框,我想合并位置列具有重复值的行,但合并性能列的值,同时保持纬度和经度值。我该怎么做?
按感兴趣的列(location
、latitude
、longitude
)分组并求和:
df.groupby(['location', 'latitude', 'longitude']).sum()
如果您不想在索引中保留分组列,请使用 .reset_index()
跟进:
df.groupby(['location', 'latitude', 'longitude']).sum().reset_index()
这取决于您想对性能列做什么。你说你想合并这些值?我假设您的意思是“总结”这些值。
如果你有这样的 df:
您需要按位置、纬度和经度分组。总结表现。
df.groupby(['location', 'lat', 'lng']).performances.sum().reset_index()