Python -pandas:如何同时选择大数据框的所有偶数列 [英] Python -pandas: How can I simultaneously select all even columns of a big Dataframe

查看:76
本文介绍了Python -pandas:如何同时选择大数据框的所有偶数列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常大的数据框,说df

I have a very large dataframe say df

df = pd.DataFrame(np.random.randint(0,100,size=(100, 16)), columns=list('asdfghjklzxcvbnm'))

我想选择所有偶数列,例如[s,f,h,...],而无需单独调用它们.我该如何在python中这样做?

I want to select all even columns such as [s,f,h,...] without calling them individually. How may I do so in python?

最佳

推荐答案

或者,您可以使用

Alternatively you can use .loc[] indexer:

In [117]: df.loc[:, ::2]
Out[117]:
     a   d   g   j   l   x   v   n
0   43  40  74  64   6  93  91  42
1   38  74  64  33   2  97  33  69
2   86  33  72  94  96  84  81  79
3   93  78   0  21  86  82  71  67
4   69  98  46  68  62  73  79  18
5   30  15  93  63  42  76   7  40
6   43  55  23  63  61  75  51  45
7   57  46  28  19  75  77  39  65
8    4  12  87  71  18  62  27  62
9    4  57  37  84  10  12   1   3
10  68  53   0  59  85  42  88  31
11  77  65  29  34  59  50  71  66
12  68   1  27  66  62  61  82  22
13  56  69  56  95  51  82  60   6
14  14  95  73  41  57  44  66   4
15  47  94  66  98  48  24  67  41
16  30  81  59  78  97  45  92  66
17  39  60  10   8   3  13  99  77
18  30  24  82  98  94  90  42  48
19  18  31  15  89  73  94  61  41
20  70  37  60  24  57  39  17  96
21  41  11  54  51  38  18  33   5
22   4  65   0  24  67  58  64  94
23  32  21   8  23  66  61  57  10
24  92   9  59  83  44  16  51  64
25  40  92  42  32  77  73  46  73
26  92  11  72  23  68  79  85   9
27  50  98  27  68  85  33   1  25
28  38  71  95   3  93  83  19   6
29  22  70  73  90   3  83  10  74
..  ..  ..  ..  ..  ..  ..  ..  ..

这篇关于Python -pandas:如何同时选择大数据框的所有偶数列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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