2017-04-13 13 views
1

は、このデータフレームを取ることができます:列と行を旋回した後パンダ:インデックスラベルのリストを提供して行をソートするネイティブな方法はありますか?

import pandas as pd 
L0 = ['d','a','b','c','d','a','b','c','d','a','b','c'] 
L1 = ['z','z','z','z','x','x','x','x','y','y','y','y'] 
L2 = [1,6,3,8,7,6,7,6,3,5,6,5] 
df = pd.DataFrame({"A":L0,"B":L1,"C":L2}) 
df = df.pivot(columns="A",index="B",values="C") 

はアルファベット順です。列の並べ替え

は簡単で、列ラベルのカスタムリストで行うことができます。

df = df[['d','a','b','c']] 

しかし、行を並べ替えるには、そのような直接的な機能を持っていない、私は考えることができる最もエレガントな方法は、使用していました列ラベルの機能と移調前と後ろ:

df = df.T[['z','x','y']].T 

df.loc[['x','y','z'],:] = df.loc[['z','x','y'],:] 

インデックスラベルのカスタムリストを提供することで、データフレームの行を並べ替える直接の方法はありませんか?

+2

でそれを行うための高速な方法、 'x'、 'y']、:] ' – ssm

答えて

3

を使用できreindexまたはreindex_axis、より高速locとしてあるもの:ssmとして

idx = ['z','x','y'] 
df = df.reindex_axis(idx) 
print (df) 
A a b c d 
B    
z 6 3 8 1 
x 6 7 6 7 
y 5 6 5 3 

が指摘:

indexの場合:

idx = ['z','x','y'] 
df = df.reindex(idx) 
print (df) 
A a b c d 
B    
z 6 3 8 1 
x 6 7 6 7 
y 5 6 5 3 

それとも

列について

cols = ['d','a','b','c'] 
df = df.reindex(columns=cols) 
print (df) 
A d a b c 
B    
x 7 6 7 6 
y 3 5 6 5 
z 1 6 3 8 

cols = ['d','a','b','c'] 
df = df.reindex_axis(cols, axis=1) 
print (df) 
A d a b c 
B    
x 7 6 7 6 
y 3 5 6 5 
z 1 6 3 8 

両方:

idx = ['z','x','y'] 
cols = ['d','a','b','c'] 
df = df.reindex(columns=cols, index=idx) 
print (df) 
A d a b c 
B    
z 1 6 3 8 
x 7 6 7 6 
y 3 5 6 5 

タイミング

In [43]: %timeit (df.loc[['z', 'x', 'y'], ['d', 'a', 'b', 'c']]) 
1000 loops, best of 3: 653 µs per loop 

In [44]: %timeit (df.reindex(columns=cols, index=idx)) 
1000 loops, best of 3: 402 µs per loop 

のみインデックス:

In [49]: %timeit (df.reindex(idx)) 
The slowest run took 5.16 times longer than the fastest. This could mean that an intermediate result is being cached. 
1000 loops, best of 3: 271 µs per loop 

In [50]: %timeit (df.reindex_axis(idx)) 
The slowest run took 6.50 times longer than the fastest. This could mean that an intermediate result is being cached. 
1000 loops, best of 3: 252 µs per loop 


In [51]: %timeit (df.loc[['z', 'x', 'y']]) 
The slowest run took 5.51 times longer than the fastest. This could mean that an intermediate result is being cached. 
1000 loops, best of 3: 418 µs per loop 

In [52]: %timeit (df.loc[['z', 'x', 'y'], :]) 
The slowest run took 4.87 times longer than the fastest. This could mean that an intermediate result is being cached. 
1000 loops, best of 3: 542 µs per loop 

def pir(df): 
    idx = ['z','x','y'] 
    a = df.index.values.searchsorted(idx) 
    df = pd.DataFrame(
     df.values[a], 
     df.index[a], df.columns 
    ) 
    return df 

In [63]: %timeit (pir(df)) 
The slowest run took 7.75 times longer than the fastest. This could mean that an intermediate result is being cached. 
10000 loops, best of 3: 91.8 µs per loop 
+0

あなたは単に 'df.loc [[' z '、' x '、' y ']]と比較することはできません。 – piRSquared

+0

同じですが、 'loc'は遅いです。私は 'reindex'と' reindex_axis'が主に実装されているので、 'loc'が選択のためにもっと実装されていると思います。 – jezrael

+0

それは理にかなっています! – piRSquared

1

locを使用すると、あなたは

df = df.loc[['z', 'x', 'y']] 

バックデータフレームに割り当てることができ、それを

df.loc[['z', 'x', 'y']] 

A d a b c 
B    
z 1 6 3 8 
x 7 6 7 6 
y 3 5 6 5 

を行うには非常に自然な方法であります

あなただけの `DF = df.loc [[ 'Z' を実行することができますloc

df.loc[['z', 'x', 'y'], ['d', 'a', 'b', 'c']] 

A d a b c 
B    
z 1 6 3 8 
x 7 6 7 6 
y 3 5 6 5 

と一度で両軸numpy.searchsorted

l = list('zxy') 
a = df.index.values.searchsorted(l) 
pd.DataFrame(
    df.values[a], 
    df.index[a], df.columns 
) 

A d a b c 
B    
z 1 6 3 8 
x 7 6 7 6 
y 3 5 6 5 
関連する問題