且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python等效于R c()函数,用于数据框列索引吗?

更新时间:2022-12-13 15:19:51

等效项是numpy的 r_ .它合并整数切片,而无需为每个切片调用范围:

The equivalent is numpy's r_. It combines integer slices without needing to call ranges for each of them:

np.r_[2:4, 7:11, 21:25]
Out: array([ 2,  3,  7,  8,  9, 10, 21, 22, 23, 24])


df = pd.DataFrame(np.random.randn(1000))
df.iloc[np.r_[2:4, 7:11, 21:25]]
Out: 
           0
2   2.720383
3   0.656391
7  -0.581855
8   0.047612
9   1.416250
10  0.206395
21 -1.519904
22  0.681153
23 -1.208401
24 -0.358545