【问题标题】:limit pandas .loc method output within a iloc range将 pandas .loc 方法输出限制在 iloc 范围内
【发布时间】:2021-08-15 04:37:59
【问题描述】:

我正在寻找我的 pandas 数据框中的最大值,但仅限于特定索引范围内:

df.loc[df['Score'] == df['Score'].iloc[430:440].max()]

这给了我一个带有多行的 pandas.core.frame.DataFrame 类型的输出。 我特别需要 iloc[430:440] 中最大值的索引整数,并且只有最大值出现的第一个索引。

有没有办法限制.loc方法的范围?

谢谢

【问题讨论】:

标签: python pandas


【解决方案1】:

如果你只想要索引:

i = df['Score'].iloc[430:440].idxmax()

如果你也想获取该行:

df.loc[i]

如果您想获取整个数据帧中具有该值的第一行(而不是仅在您最初指定的范围内):

df[df['Score'] == df['Score'].iloc[430:440].max()].iloc[0]

【讨论】:

    猜你喜欢
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 2015-04-29
    • 2016-10-21
    • 1970-01-01
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多