gpt4 book ai didi

python - Pandas - 选择具有最佳值的行

转载 作者:太空宇宙 更新时间:2023-11-03 14:10:47 25 4
gpt4 key购买 nike

我有这个数据框

   col1 col2 col3
0 2 A 1
1 1 A 100
2 3 B 12
3 4 B 2

我想从所有 A 中选择最高的 col1 值,然后从所有 B 中选择一个,等等,即这是所需的输出

   col1 col2  col3
0 2 A 1
3 4 B 2

我知道我需要某种 groupby('col2'),但我不知道之后该用什么。

最佳答案

这是你想要的吗?

In [16]: df.groupby('col2').max().reset_index()
Out[16]:
col2 col1
0 A 2
1 B 4

关于python - Pandas - 选择具有最佳值的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38317041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com