Python equivalent of R c() function, for dataframe column indices?

后端 未结 3 1904
面向向阳花
面向向阳花 2021-01-13 14:56

I would like to select from a pandas dataframe specific columns using column index.

In particular, I would like to select columns index by the column index generate

3条回答
  •  伪装坚强ぢ
    2021-01-13 15:19

    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
    

提交回复
热议问题