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

Python: Extract different columns and assign to a new dataframe

I have this df:

A  B  C  D
1  2  3  4

I want to extract columns 0:1 and 3rd one

But first thought would be:

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

X = DS.iloc[:, 0:1:3].values

But, this approach does not work

>Solution :

If really you need to work with ranges, use numpy.r_ to combine them:

out = df.iloc[:, np.r_[0:2,3]]

If you know the columns, be explicit:

out = df[['A', 'B', 'D']]

Output:

   A  B  D
0  1  2  4
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