简体   繁体   中英

Selecting multiple columns, both consecutive and non-consecutive, in a Pandas dataframe

I am trying to run the following:

X = d.iloc[:, [13, 30, 35:45]].values

It fails at the range 35:45.

PS: There is this question with many useful answers, but they don't address the issue of getting both consecutive and non-consecutive columns.

Use np.r_ :

import numpy as np
X = d.iloc[:, np.r_[13, 30, 35:45]].to_numpy()

Intermediate output of np.r_[13, 30, 35:45] :

array([13, 30, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM