检查最大日期并仅保留该行

问题描述 投票:0回答:1

我的 df 在 B 列中有几个重复的值。 我需要的是为 B 列中的每个值查找 A 列的最新日期,并重新选择不是最新的行:

A            B          E
26/12/2023  apple         7,9
26/12/2022  apple         8,3
26/12/2023  pear          28,6
26/12/2022  orange        33,3
26/12/2023  wildberry     24,7
26/12/2022  wildberry     29,1
26/12/2023  grapes        17,1

结果应该是:

A            B          E
26/12/2023  apple          7,9
26/12/2023  pear          28,6
26/12/2022  orange        33,3
26/12/2023  wildberry     24,7
26/12/2023  grapes        17,1

你能帮我找到正确的公式吗?我是初学者,迷失在 loc 函数中

我是初学者,迷失在 loc 函数中

python date max lines-of-code
1个回答
0
投票

您可以使用分组依据:

df.groupby('B')['A'].max()

https://scales.arabpsychology.com/stats/how-to-find-the-max-value-by-group-in-pandas/

© www.soinside.com 2019 - 2024. All rights reserved.