简体   繁体   English

从 pandas dataframe 中选择不连续的和连续的列

[英]Selecting non-consecutive and consecutive columns from a pandas dataframe

I'm trying to select multiple columns from a pandas DataFrame but am having trouble doing so.我正在尝试 select 来自 pandas DataFrame 的多个列,但这样做有困难。 Suppose I have the following DataFrame:假设我有以下 DataFrame:

import pandas as pd
import numpy as np

cols = ['test','one','two','three','four','five','six','seven','eight','nine','ten']
df = pd.DataFrame(np.random.rand(10,11).round(2),columns=cols)

I want to select columns test , two , four , five , six , seven , eight我要select列testtwofourfivesixseveneight

I know that if I want to select individual columns,我知道如果我想 select 个别列,

df[['test','two']]

and if I want to select consecutive columns,如果我想 select 连续列,

df.loc[:,'four':'eight']

work just fine but how to I combine the two concisely?工作得很好,但我如何简洁地将两者结合起来?

I realize that for this specific example, writing我意识到对于这个具体的例子,写作

df[['test', 'two', 'four', 'five', 'six', 'seven', 'eight']]

works too but I want to know if there is a way to make use of the fact that most of the columns are consecutive here to save some time writing them all.也可以,但我想知道是否有办法利用大多数列在这里是连续的这一事实来节省一些时间来写它们。

np.r_ as @Pooja suggested but with get_loc and get_indexer for label based slicing: np.r_正如@Pooja建议的那样,但使用get_locget_indexer用于基于 label 的切片:

a = ['test','two']
b = ['four','eight']
idx= np.r_[df.columns.get_indexer(a),df.columns.get_loc(b[0]):df.columns.get_loc(b[1])+1]
print(df.iloc[:,idx])

   test   two  four  five   six  seven  eight
0  0.11  0.91  0.13  0.99  0.17   0.56   0.21
1  0.70  0.94  0.72  0.48  0.53   0.99   0.27
2  0.37  0.03  0.81  0.18  0.47   0.94   0.77
3  0.13  0.69  0.16  0.80  0.02   0.42   0.48
4  0.79  0.91  0.97  0.83  0.20   0.32   0.58
5  0.12  0.86  0.44  0.01  0.71   0.65   0.03
6  0.77  0.31  0.21  0.73  0.70   0.95   0.11
7  0.09  0.91  0.45  0.35  0.91   0.21   0.92
8  0.28  0.32  0.73  0.93  0.97   0.03   0.93
9  0.55  0.77  0.02  0.18  0.65   0.50   0.85

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM