[英]Selecting multiple columns, both consecutive and non-consecutive, in a Pandas dataframe
[英]Selecting non-consecutive and consecutive columns from a pandas dataframe
我正在尝试 select 来自 pandas 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)
我要select列test
, two
、 four
、 five
、 six
、 seven
、 eight
我知道如果我想 select 个别列,
df[['test','two']]
如果我想 select 连续列,
df.loc[:,'four':'eight']
工作得很好,但我如何简洁地将两者结合起来?
我意识到对于这个具体的例子,写作
df[['test', 'two', 'four', 'five', 'six', 'seven', 'eight']]
也可以,但我想知道是否有办法利用大多数列在这里是连续的这一事实来节省一些时间来写它们。
np.r_
正如@Pooja建议的那样,但使用get_loc
和get_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.