Selecting multiple columns R vs python pandas

牧云@^-^@ 提交于 2020-01-30 03:31:28

问题


I am an R user who is currently learning Python and I am trying to replicate a method of selecting columns used in R into Python.

In R, I could select multiple columns like so:

df[,c(2,4:10)]

In Python, I know how iloc works, but I couldn't split between a single column number and a consecutive set of them.

This wouldn't work

df.iloc[:,[1,3:10]]

So, I'll have to drop the second column like so:

df.iloc[:,1:10].drop(df.iloc[:,1:10].columns[1] , axis=1)

Is there a more efficient way of replicating the method from R in Python?


回答1:


You can use np.r_ that accepts mixed slice notation and scalar indices and concatenate them as 1-d array:

import numpy as np
df.iloc[:,np.r_[1, 3:10]]

df = pd.DataFrame([[1,2,3,4,5,6]])

df

#   0   1   2   3   4   5
#0  1   2   3   4   5   6

df.iloc[:, np.r_[1, 3:6]]

#   1   3   4   5
#0  2   4   5   6

As np.r_ produces:

np.r_[1, 3:6]
# array([1, 3, 4, 5])


来源:https://stackoverflow.com/questions/46576519/selecting-multiple-columns-r-vs-python-pandas

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