使用iloc删除各种列 [英] dropping various columns using iloc

查看:548
本文介绍了使用iloc删除各种列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的数据框 df1 包含一堆列:

Say I have a dataframe df1 with a bunch of columns:

Index(['Open', 'High', 'Low', 'Close', 'Volume', 'Adj Close', 'log_return',
   'close_open', 'Daily_Change', '30_Avg_Vol', '20_Avg_Vol', '15_Avg_Vol',
   '9_Avg_Vol', 'changed', 'p_changed', '5days', '7days',
   '7days_10daysAvg', '5_7days_Avg', '-2_before', '-1_before', '0_before',
   '1_before', '2_before', '3_before', '4_before', '5_before', '6_before',
   'Bullish'],
  dtype='object')

我想创建一个新的数据框,保留一些列并删除其他列。我想保留0-13,13-19,19-28列(使用偏移表示法)

I want to create a new dataframe that keeps some of the columns and drops others. I want to keep columns 0-13, 13-19, 19-28 (using offset notation)

我尝试了这些的变体

x2 =df.iloc[:-5, [0:13,13:19, 19:28 ]]     # meant to edit 13:19 in this line
x2 =df.iloc[:-5, [[0:13],[13:19], [19:28 ]]

但没有运气。我得到 syntaxerror

推荐答案

使用 np.r _

df.iloc[:-5, np.r_[0:13, 13:19, 19:28]]

这篇关于使用iloc删除各种列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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