How to select multiple columns in a data frame that has many columns (>100)?

Question:

Suppose there is a data frame that has 120 columns. I need only column with numbers 2-50, 59-84, and 95-110. I tried this code. And it doesnt work.

df.iloc[:,[2:50,59:84,95:110]]

Asked By: Cecilia

||

Answers:

try this

df.iloc[:, np.r_[2:50,59:84,95:110]]

This worked for me:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.rand(5,60))
print(df.iloc[:, np.r_[1:3, 5:8]])

Output:

          1         2         5         6         7
0  0.830642  0.054954  0.686140  0.472132  0.227086
1  0.634129  0.304420  0.688517  0.192814  0.291788
2  0.697258  0.332265  0.305456  0.900968  0.657861
3  0.616603  0.382215  0.794018  0.053626  0.952454
4  0.568792  0.553030  0.761718  0.780927  0.253707

Or this (same output)

cols = list(range(1,3)) + list(range(5,8))
print(df.iloc[:, cols])
Answered By: perpetualstudent
df.iloc[:, lambda df: [*range(2, 50)] + [*range(59, 84)] + [*range(95, 110)]
Answered By: the_pr0blem
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.