python - 获取前一个较小值的索引

标签 python pandas dataframe

我有一个如下所示的数据框:

index value
0     1
1     1
2     2
3     3
4     2
5     1
6     1

我想要的是让每个值返回前一个较小值的索引,此外,还返回前一个“1”值的索引。如果值为 1 我不需要它们(两个值都可以是 -1 或其他)。

所以我追求的是:

index value  previous_smaller_index  previous_1_index
0     1            -1                      -1
1     1            -1                      -1
2     2             1                       1
3     3             2                       1
4     2             1                       1
5     1            -1                      -1
6     1            -1                      -1

我尝试使用滚动、累积函数等,但我无法弄明白。 任何帮助将不胜感激!

编辑: SpghttCd 已经为“previous 1”问题提供了一个很好的解决方案。我正在为“以前的小”问题寻找一个不错的 pandas one liner。 (当然,尽管这两个问题都欢迎更好、更高效的解决方案)

最佳答案

  • “previous_smaller_index”可以使用矢量化 numpy 广播比较与 argmax 找到。

  • “previous_1_index”可以在 cumsummed 掩码上使用 groupbyidxmax 来解决。

m = df.value.eq(1)
u = np.triu(df.value.values < df.value[:,None]).argmax(1)
v = m.cumsum()

df['previous_smaller_index'] = np.where(m, -1, len(df) - u - 1)
df['previous_1_index'] = v.groupby(v).transform('idxmax').mask(m, -1)

df
   index  value  previous_smaller_index  previous_1_index
0      0      1                      -1                -1
1      1      1                      -1                -1
2      2      2                       1                 1
3      3      3                       2                 1
4      4      2                       1                 1
5      5      1                      -1                -1
6      6      1                      -1                -1

如果你想把这些作为一个衬里,你可以把几行揉成一行:

m = df.value.eq(1)
df['previous_smaller_index'] = np.where(
    m, -1, len(df) - np.triu(df.value.values < df.value[:,None]).argmax(1) - 1
)[::-1]

# Optimizing @SpghttCd's `previous_1_index` calculation a bit
df['previous_1_index'] = (np.where(
    m, -1, df.index.where(m).to_series(index=df.index).ffill(downcast='infer'))
)

df

   index  value  previous_1_index  previous_smaller_index
0      0      1                -1                      -1
1      1      1                -1                      -1
2      2      2                 1                       1
3      3      3                 1                       2
4      4      2                 1                       1
5      5      1                -1                      -1
6      6      1                -1                      -1

整体表现

设置和性能基准测试是使用 perfplot 完成的。代码可以在 this gist 找到.

enter image description here

时间是相对的(y 尺度是对数的)。


previous_1_index 性能

Gist with relevant code.

enter image description here

关于python - 获取前一个较小值的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54190261/

相关文章:

python - Pandas Groupby : Count and mean combined

python - 如何访问 pandas 中的多索引级别?

python - 在 pandas 数据框中创建累积计数列

python - 如何检查与 perforce 中某个标签关联的文件是否同步?

python - 导入错误 : No module named xxx after setup.

python - 多索引计算到新列

python - 使用工作日或添加工作日创建数据框

python - 将数据添加到预训练模型中

python - VS Code 在 python 2.7 中的打印语句中显示错误消息

python - Pandas 不具备过滤条件