vahid vhr
vahid vhr

Reputation: 5

select multiple separately columns by iloc in pandas

my file contains 15 columns and 10 rows. I want to select some columns between them like this (1:5 and 7:11 and 13:15). I wrote it similer to:

'df.iloc[: ,[1:6,7:12,13:]]'

or

'df.iloc[: ,[[1:6],[7:12],[13:]]]'

but none of them worked. I have challenge.

may someone help?

Upvotes: 0

Views: 3979

Answers (1)

Corralien
Corralien

Reputation: 120549

You can use np.r_ to have slice notation:

df = pd.DataFrame(columns=list('ABCDEFGHIJKLMNOPQRSTUVWXYZ'))
df1 = df.iloc[:, np.r_[1:5, 7:11, 13:15]]
print(df1)

# Output
Empty DataFrame
Columns: [B, C, D, E, H, I, J, K, N, O]
Index: []

Upvotes: 3

Related Questions