按更多索引范围(例如 10:12
和 25:28
)对数据帧进行切片的 pythonic 方法是什么?
我想要一个更优雅的方式:
df = pd.DataFrame({'a':range(10,100)})
df.iloc[[i for i in range(10,12)] + [i for i in range(25,28)]]
结果:
a
10 20
11 21
25 35
26 36
27 37
这样的事情会更优雅:
df.iloc[(10:12, 25:28)]
原文由 ragesz 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以使用 numpy 的
r_
“切片技巧”:注意:这现在给出警告
The pandas.np module is deprecated and will be removed from pandas in a future version. Import numpy directly instead
。为此,您可以import numpy as np
然后按以下方式切片:这给出: