获取使用Python Pandas在每个组中具有最大值的行

发布于 2025-02-04 15:44:45 字数 1491 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱,才寂寞 2025-02-11 15:44:45

快速的Google搜索,您将找出如何获得相关行在这里
对于您的实现:

df.groupby("ex")['transaction_fee'].max()

您应该寻找的另一件事是如何获取原始表的相应索引。您可以找到类似的东西 a>。
为您的实施:

idxs = df.groupby("ex")['transaction_fee'].transform(max) == df['transaction_fee']
relevant_df = df[idxs]

A quick google search and you will find out how to get relevant rows here.
For your implementation:

df.groupby("ex")['transaction_fee'].max()

The other thing you should look for is how to get the corresponding indices for the original table. you could find something similar here.
For your implementation:

idxs = df.groupby("ex")['transaction_fee'].transform(max) == df['transaction_fee']
relevant_df = df[idxs]
凑诗 2025-02-11 15:44:45

通过一个列的所有列值和另一列的最大值获取所有列值。使用IDXMAX()iLoc()

idxmax()将返回最大值的索引,而不是最大值本身。

iLoc()基于索引,返回DF的选定部分。
我们可以使用ID来过滤记录。

idxs = df.groupby("ex")['transaction_fee'].idxmax()
df = df.iloc[idxs]

For getting all the column values for a group by of one column and max value of another column. Use idxmax() and iloc()

idxmax() will return the index of the maximum value, not the maximum value itself.

iloc() Index based, returns the selected section of df.
We can use the ids to filter the records.

idxs = df.groupby("ex")['transaction_fee'].idxmax()
df = df.iloc[idxs]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文