'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?
Solution 1:[1]
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: []
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Corralien |
