Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Multiple selection of dataframe using multiple column slices

I have a dataframe that has 10 columns, I am trying select columns 1 to 3 (first to third) and columns 5 to 8 (fifth to eighth). Is it possible to select using the iloc selector so that it can take multiple slices. The sample below can slice first to third, I would like to add the columns 5 to 8 as well

df = pc_df.iloc[:,0:4]

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use numpy:

#import  numpy as np
df.iloc[:,np.r_[0:4, 5:9]]

np.r_ will concatenate the indexes for you.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading