是否存在使用 iloc 或其他一些功能对两个或多个范围进行切片的简写?
例如,如果我想从pandas数据框中获取5到10列以及15到25列,我将如何实现?
答案 0 :(得分:1)
numpy.r_
将为您带来结果。最终它将创建一个数组用作索引。
df = pd.DataFrame(np.arange(200).reshape(5, 40))
df.iloc[:, np.r_[5:10, 15:25]]
+---+------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| | 5 | 6 | 7 | 8 | 9 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 |
+---+------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 0 | 5 | 6 | 7 | 8 | 9 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 1 | 45 | 46 | 47 | 48 | 49 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 |
| 2 | 85 | 86 | 87 | 88 | 89 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 |
| 3 | 125 | 126 | 127 | 128 | 129 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 |
| 4 | 165 | 166 | 167 | 168 | 169 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 |
+---+------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
答案 1 :(得分:0)
我认为您可以只使用.columns.tolist()
并根据需要将列表组合在一起。
df = pd.DataFrame({f'col_{i}': [num for num in np.random.randint(0, 10, 5)] for i in range(20)})
print(df)
col_0 col_1 col_2 col_3 ... col_16 col_17 col_18 col_19
0 0 0 9 0 ... 3 3 5 8
1 7 1 9 2 ... 5 6 9 7
2 0 9 9 6 ... 2 1 4 8
3 0 7 4 7 ... 0 4 7 1
4 8 7 8 5 ... 1 9 9 6
[5 rows x 20 columns]
print(df[df.columns.tolist()[5:10] + df.columns.tolist()[15:20]])
col_5 col_6 col_7 col_8 ... col_16 col_17 col_18 col_19
0 2 7 4 9 ... 3 3 5 8
1 9 2 8 5 ... 5 6 9 7
2 2 9 0 7 ... 2 1 4 8
3 1 2 2 9 ... 0 4 7 1
4 1 7 8 0 ... 1 9 9 6
[5 rows x 10 columns]