本文介绍了Python等效于R c()函数,用于数据框列索引吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用列索引从pandas数据框特定的列中进行选择.

I would like to select from a pandas dataframe specific columns using column index.

尤其是,我想通过R中c(12:26,69:85,96:99,134:928,933:935,940:967)生成的列索引选择列索引.我想知道如何在Python中做到这一点?

In particular, I would like to select columns index by the column index generated by c(12:26,69:85,96:99,134:928,933:935,940:967) in R. I wonder how can I do that in Python?

我在想类似下面的内容,但是,当然,python没有名为c()的函数.

I am thinking something like the following, but of course, python does not have a function called c()...

input2 = input2.iloc[:,c(12:26,69:85,96:99,134:928,933:935,940:967)]

推荐答案

等效项是numpy的 r_ .它合并整数切片,而无需为每个切片调用范围:

The equivalent is numpy's r_. It combines integer slices without needing to call ranges for each of them:

np.r_[2:4, 7:11, 21:25]
Out: array([ 2,  3,  7,  8,  9, 10, 21, 22, 23, 24])


df = pd.DataFrame(np.random.randn(1000))
df.iloc[np.r_[2:4, 7:11, 21:25]]
Out:
           0
2   2.720383
3   0.656391
7  -0.581855
8   0.047612
9   1.416250
10  0.206395
21 -1.519904
22  0.681153
23 -1.208401
24 -0.358545

这篇关于Python等效于R c()函数,用于数据框列索引吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 15:03