在满足条件的情况下,获取最后一行大熊猫

时间:2018-04-17 18:32:21

标签: python pandas indexing

+------------+----+----+----+-----+----+----+----+-----+
|     WS     | 1  | 2  | 3  |  4  | 1  | 2  | 3  |  4  |
+------------+----+----+----+-----+----+----+----+-----+
| w1         |  0 |  0 |  0 |  50 |  0 |  0 |  0 |  50 |
| w2         |  0 | 30 |  0 |   0 |  0 | 30 |  0 |   0 |
| d1         |  0 |  0 |  0 |   0 |  0 |  0 |  0 |   0 |
| d2         | 62 |  0 |  0 |   0 | 62 |  0 |  0 |   0 |
| Total      | 62 | 30 |  0 |  50 | 62 | 30 |  0 |  50 |
| Cumulative | 62 | 92 | 92 | 142 | 62 | 92 | 92 | 142 |
+------------+----+----+----+-----+----+----+----+-----+

根据值大于0的列的条件,我想获得row" Cumulative"的相应值。

如图所示,当50> 0,我想得到相应的"累积"价值142。

+------------+----+----+---+-----+----+----+---+-----+
|     WS     | 1  | 2  | 3 |  4  | 1  | 2  | 3 |  4  |
+------------+----+----+---+-----+----+----+---+-----+
| Cumulative | 62 | 92 | 0 | 142 | 62 | 92 | 0 | 142 |
+------------+----+----+---+-----+----+----+---+-----+

我尝试了pandas loc和iloc,但他们无法执行我想要的任务。

先谢谢你了!

1 个答案:

答案 0 :(得分:0)

你绝对应该更好地设计你的问题,但无论如何,这是一个可能的解决方案:

假设 df 是您的DataFrame:

df[df>5].loc['Cumulative'].dropna()