3

I'm working on a dataset (Rows:5000 and Columns: 60). I want to read some of the columns which are related to my analysis but the code doesn't work.

Column 1, column 5, columns 22 to 28 and columns 47 to 54.

I've read the manual and it seems just I can select the number of columns one by one or range not both of them. Could you please let me know how can I solve this problem?

All_Dataset = pd.read_csv('Data.csv', engine = 'python')
data = All_Dataset.iloc[:, [0, 5,  22:29, 47:55]]
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
Hamed
  • 43
  • 3
  • 9
  • 1
    The package you're using (pandas as far as I can tell) is not part of python's standard library. When you have a question on a third-part package, please mention it in the tags and preferably in your question too. – bruno desthuilliers Dec 28 '18 at 15:48

2 Answers2

3

You could create a list with the indices by chaining the iterables:

import numpy as np
import pandas as pd
from itertools import chain

# create sample data-frame
data = np.random.randint(1, 10, size=(10, 100))
df = pd.DataFrame(data=data)

# create indices
indices = list(chain([0, 5], range(22, 29), range(47, 55)))

result = df.iloc[:, indices]
print(result.shape)

Output

(10, 17)
Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76
3

Using np.r_

import numpy as np

df.iloc[:,np.r_[0, 5,  22:29, 47:55]]
BENY
  • 317,841
  • 20
  • 164
  • 234