Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 切片多个区域_Python_Database_Pandas_Dataframe_Slice - Fatal编程技术网

Python 切片多个区域

Python 切片多个区域,python,database,pandas,dataframe,slice,Python,Database,Pandas,Dataframe,Slice,是否有使用iloc或其他功能对两个或多个范围进行切片的速记 例如,如果我想从pandas数据帧中获取5到10的列,以及15到25的列,我将如何实现这一点?将得到结果。它最终创建了一个数组用作索引 df = pd.DataFrame(np.arange(200).reshape(5, 40)) df.iloc[:, np.r_[5:10, 15:25]] +---+------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----

是否有使用iloc或其他功能对两个或多个范围进行切片的速记

例如,如果我想从pandas数据帧中获取5到10的列,以及15到25的列,我将如何实现这一点?

将得到结果。它最终创建了一个数组用作索引

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 |
+---+------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+

我认为您可以使用
.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]