python - Pandas:如何检查列是否包含值 0 然后根据某种规则对所选行数据进行排序?

标签 python pandas numpy sorting

我有一个数据框

Testcase   Processing_time   Pass   Fail   avg_failure_rate   Ranking_value
   t1         1.102088        8    26        76.47           69.38
   t2         1.718864        19    3        13.63           7.93
   t3         25              22    0         0               0
   t4         15              22    0         0               0

我想保留上面数据框中的前两个测试用例,但我想根据最短处理时间列对其余测试用例进行排序。

期望的输出:

TestCase   Processing_time  Pass  Fail  avg_failure_rate  Ranking_value
   t1         1.102088        8    26        76.47           69.38
   t2         1.718864        19    3        13.63           7.93
   t4         15              22    0         0               0
   t3         25              22    0         0               0

如果测试用例的排名值等于0,则应根据最短处理时间规则对它们进行排序。 有什么办法可以实现这一点吗?

最佳答案

使用 .loc 筛选排名值为 0 的过滤器并按 .sort_values() 排序。然后将排名值不等于 0 的其他部分追加到 .append() ,如下:

df.loc[df['Ranking_value'] != 0].append(df.loc[df['Ranking_value'] == 0].sort_values('Processing_time'))

结果:

  Testcase  Processing_time  Pass  Fail  avg_failure_rate  Ranking_value
0       t1         1.102088     8    26             76.47          69.38
1       t2         1.718864    19     3             13.63           7.93
3       t4        15.000000    22     0              0.00           0.00
2       t3        25.000000    22     0              0.00           0.00

关于python - Pandas:如何检查列是否包含值 0 然后根据某种规则对所选行数据进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69652119/

相关文章:

python - Pandas 数据框 : How to parse integers into string of 0s and 1s?

python - 如何反向执行 date_range?

python - 以统一的方式缩放值?

python - 使用 Python(加密)生成 CSR

Python If 语句在测试 1 到 10 之间的数字时不起作用

python - 在 pandas 中优先过滤的最佳方法是什么?

python - 如何在没有 root 访问权限的情况下安装 python 模块?

python - 定义具有五个峰值的 2D 高斯概率

python - 简单非本地绑定(bind)的 NameError

python - 在没有 for 循环的情况下对多维数组的时间序列进行去趋势化