2017-05-20 70 views
1

例如,如何獲得一個數據幀的多列切片在大熊貓

from pandas import DataFrame 
df = DataFrame(np.arange(8).reshape(1, 8), columns = list('abcdefgh')) 

我要選擇的列「B」:「d」和「F」:「H」,說都包括。

我知道我可以選擇「B」:通過執行「d」:

df2 = df.loc[:, 'b':'d'],但類似df2 = df.loc[:, ['b':'d', 'f':'h']] 這就好比在MATLAB語法不會在這裏工作,所以我怎麼能選擇幾個 數據框中的列?

回答

1

可以使用numpy.r_爲指數的級聯,但它僅適用於位置,因此需要get_locsearchsorted + iloc

df = pd.DataFrame(np.arange(8).reshape(1, 8), columns = list('abcdefgh')) 
print (df) 
    a b c d e f g h 
0 0 1 2 3 4 5 6 7 

b = df.columns.get_loc('b') 
d = df.columns.get_loc('d') 
f = df.columns.get_loc('f') 
h = df.columns.get_loc('h') 
print (b,d,f,h) 
1 3 5 7 
b = df.columns.searchsorted('b') 
d = df.columns.searchsorted('d') 
f = df.columns.searchsorted('f') 
h = df.columns.searchsorted('h') 
print (b,d,f,h) 
1 3 5 7 

df = df.iloc[:, np.r_[b:c+1, f:h+1]] 
print (df) 
    b c d f g h 
0 1 2 3 5 6 7 

是一樣的:

df = df.iloc[:, np.r_[1:4, 5:8]] 
print (df) 
    b c d f g h 
0 1 2 3 5 6 7 

df = df.iloc[:, np.r_['b':'d', 'f':'h']] 
print (df) 
#TypeError: unsupported operand type(s) for -: 'str' and 'str' 

loc + join另一種解決方案:

df = df.loc[:,'b':'d'].join(df.loc[:,'f':'h']) 
print (df) 
    b c d f g h 
0 1 2 3 5 6 7 
0

另一種方式來做到這一點使用pd.concat:

pd.concat([df.loc[:,'b':'d'],df.loc[:,'f':'h']],1) 
Out[608]: 
    b c d f g h 
0 1 2 3 5 6 7