在Pandas DataFrame中选择多个列范围 [英] Select multiple ranges of columns in Pandas DataFrame

查看:520
本文介绍了在Pandas DataFrame中选择多个列范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须读取几个文件,其中一些文件采用Excel格式,有些文件采用CSV格式.有些文件有数百列.

I have to read several files some in Excel format and some in CSV format. Some of the files have hundreds of columns.

是否可以在不指定所有列名称或位置的情况下选择多个列范围?例如,选择第1 -10、15、17和50-100列:

Is there a way to select several ranges of columns without specifying all the column names or positions? For example something like selecting columns 1 -10, 15, 17 and 50-100:

df = df.ix[1:10, 15, 17, 50:100]

从Excel文件和CSV文件创建数据框时以及数据成帧器创建后,我都需要知道如何做.

I need to know how to do this both when creating dataframe from Excel files and CSV files and after the data framers created.

推荐答案

使用所以你可以做

df.iloc[:, np.r_[1:10, 15, 17, 50:100]]

这篇关于在Pandas DataFrame中选择多个列范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆