【问题标题】:How to combine rows that have the same value for specific columns?如何组合特定列具有相同值的行?
【发布时间】:2021-12-30 20:44:30
【问题描述】:

我已经合并了两个数据框,我想合并位置列具有重复值的行,但在保留纬度和经度值的同时合并性能列的值。我怎么能这样做?

【问题讨论】:

    标签: pandas dataframe merge concatenation


    【解决方案1】:

    按感兴趣的列(locationlatitudelongitude)分组并求和:

    df.groupby(['location', 'latitude', 'longitude']).sum()
    

    如果您不想将分组列保留在索引中,请使用 .reset_index() 跟进:

    df.groupby(['location', 'latitude', 'longitude']).sum().reset_index()
    

    【讨论】:

      【解决方案2】:

      这取决于您想对性能列执行什么操作。你说你想结合价值观?我假设您的意思是“总结”这些值。

      如果你有这样的 df:

      您需要按位置、纬度和经度进行分组。并总结表现。

      df.groupby(['location', 'lat', 'lng']).performances.sum().reset_index()
      

      【讨论】:

      • 谢谢,这正是我想要的
      • 如果我的回答有帮助,请将其标记为“已接受的答案”
      猜你喜欢
      • 2021-09-08
      • 2012-07-12
      • 2022-11-20
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      • 2021-09-12
      • 2018-02-09
      • 2021-12-29
      相关资源
      最近更新 更多