pandas 数据框选择多个不连续的列/切片 [英] Pandas Dataframe select multiple discontinuous columns/slices

查看:87
本文介绍了 pandas 数据框选择多个不连续的列/切片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有> 100列的数据框.我正在尝试选择0〜32和#83列.似乎1片适用于下面的代码.

I have dataframe with >100 columns. I am trying to select the columns 0~32 and #83. It seems that 1 slice works fine with the code below.

df_new = df[df.columns[0:32]]

它不适用于下面的2 slices代码.我该如何解决该问题?

It does not work with 2 slices code below though. How do I fix the problem?

df_new = df[df.columns[0:32, 83]]

推荐答案

np.r_索引器与iloc结合使用,如下所示:

Use the np.r_ indexer in conjunction with iloc, like this:

df.iloc[:, np.r_[0:32, 83]]


np.r_[0:32, 83]

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 83])

这篇关于 pandas 数据框选择多个不连续的列/切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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