选择多列R vs python pandas [英] Selecting multiple columns R vs python pandas

查看:61
本文介绍了选择多列R vs python pandas的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是R用户,目前正在学习Python,并且我正试图将选择R中使用的列的方法复制到Python中.

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.

在R中,我可以选择多个列,如下所示:

In R, I could select multiple columns like so:

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

在Python中,我知道iloc的工作原理,但是我无法在单个列号和连续的列号之间进行分割.

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

这行不通

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)

是否有更有效的方法可以在Python中从R复制该方法?

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

推荐答案

您可以使用np.r_接受混合的切片表示法和标量索引,并将它们连接为一维数组:

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

随着np.r_产生:

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

这篇关于选择多列R vs python pandas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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