Slice multiple column ranges with Pandas

て烟熏妆下的殇ゞ 提交于 2019-12-31 05:26:13

问题


Suppose I have 20 Columns in a data set and i want to use 19 as an input. and input columns are columns from 1:10 and 12: 20. and I want to use 11th column as an output. so how to give this kind of range using pandas?

for example: Example Data Set

consider above data it have 4 columns but i have to take input only 3 columns but those columns are b,d,e and i want to skip c column. Right now i m using input = dftrain.loc[:,:'e'] which consider all 4 columns.


回答1:


Option 1
np.r_

idx = np.r_[0:11, 12:20]

idx
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 12, 13, 14, 15, 16, 17,
       18, 19])

Pass this to iloc -

df.iloc[:, 11] = df.iloc[:, idx].sum(axis=1) # sum, for example

Option 2
pd.IndexSlice

idx = pd.IndexSlice[0:11, 12:20]

idx
(slice(0, 11, None), slice(12, 20, None))

You can use idx in the same manner as before.



来源:https://stackoverflow.com/questions/47966085/slice-multiple-column-ranges-with-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!