【问题标题】:keep row with highest value amongst duplicates on different columns在不同列的重复项中保留具有最高值的行
【发布时间】:2021-07-13 08:51:00
【问题描述】:

我有一个像这样的 pandas 数据框,其中我可以有相同的 long 和 lat 组合的行:

初始df:

   lon  lat         name  value protection      a      b         c  score
0   20   10       canada    563        NaN    cat    dog  elephant   20.0
1   30   10       canada     65        NaN   lion  tiger       cat   30.0
2   40   20       canada    893        NaN    dog    NaN       NaN   20.0
3   40   20          usa      4        NaN  horse  horse      lion   40.0
4   45   15          usa   8593        NaN    NaN   lion       cat   10.0
5   20   10  protection1    100     medium    NaN    NaN       NaN    NaN
6   40   20  protection1     20       high    NaN    NaN       NaN    NaN
7   50   30  protection1    500        low    NaN    NaN       NaN    NaN

但我想要的是:

想要的输出:

   lon  lat protection      a      b         c  score
0   20   10     medium    cat    dog  elephant   20.0
1   30   10        NaN   lion  tiger       cat   30.0
2   40   20       high  horse  horse      lion   40.0
3   45   15        NaN    NaN   lion       cat   10.0
4   50   30        low    NaN    NaN       NaN    NaN

输出数据框应包含具有 longlat 列的唯一组合的行,其中仅保留具有最高 score 的行,但如果 longlat 有重复项并且在protection 列这些应该合二为一

【问题讨论】:

  • 试试df.drop_duplicates(subset=['lon'],keep='last')
  • 我试过df.drop_duplicates(subset=['lon', 'lat'],keep='last'),但它去掉了太多行
  • 请将您的数据框作为文本发布,以便我们重现您的数据框或在您的问题中添加df.head(10).to_dict() 的输出
  • 我用 dfs 作为文本编辑了我的帖子

标签: pandas duplicates rows


【解决方案1】:

试试:

df = df.sort_values(by="score", ascending=False)
g = df.groupby(["lon", "lat"])
df_out = (
    g.first()
    .assign(
        protection=g.agg(
            {"protection": lambda x: ",".join(x.dropna())}
        ).replace("", np.nan)
    )
    .reset_index()
)

print(df_out)

打印:

   lon  lat         name  value protection      a      b         c  score
0   20   10       canada    563     medium    cat    dog  elephant   20.0
1   30   10       canada     65        NaN   lion  tiger       cat   30.0
2   40   20          usa      4       high  horse  horse      lion   40.0
3   45   15          usa   8593        NaN    NaN   lion       cat   10.0
4   50   30  protection1    500        low    NaN    NaN       NaN    NaN

【讨论】:

  • 这行得通!非常感谢您的帮助!
【解决方案2】:

一种方法是创建一个 func,并将您的逻辑广告应用于 groupby:

def func(df1):
    df1 = df1.fillna(method='bfill')
    df1 = df1.fillna(method='ffill')
    return df1.sort_values('score', ascending=False)[:1]

result = df.groupby(['lon', 'lat']).apply(func)

添加重置索引和选择以获得准确的发布输出:

result.reset_index(drop=True)[['lon', 'lat', 'protection', 'a', 'b', 'c', 'score']]
lon lat protection a b c score
0 20 10 medium cat dog elephant 20
1 30 10 nan lion tiger cat 30
2 40 20 high horse horse lion 40
3 45 15 nan nan lion cat 10
4 50 30 low nan nan nan nan

【讨论】:

    猜你喜欢
    • 2014-03-29
    • 2018-12-29
    • 2016-07-26
    • 2017-06-09
    • 2012-09-11
    • 1970-01-01
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多