2016-10-13 18 views
7

chcę pokroić dataframe multiindex pandypandy krojenie multiindex dataframe

Oto kod, aby uzyskać moje dane testowe:

import pandas as pd 

testdf = { 
    'Name': { 
     0: 'H', 1: 'H', 2: 'H', 3: 'H', 4: 'H'}, 'Division': { 
      0: 'C', 1: 'C', 2: 'C', 3: 'C', 4: 'C'}, 'EmployeeId': { 
       0: 14, 1: 14, 2: 14, 3: 14, 4: 14}, 'Amt1': { 
        0: 124.39, 1: 186.78, 2: 127.94, 3: 258.35000000000002, 4: 284.77999999999997}, 'Amt2': { 
         0: 30.0, 1: 30.0, 2: 30.0, 3: 30.0, 4: 60.0}, 'Employer': { 
          0: 'Z', 1: 'Z', 2: 'Z', 3: 'Z', 4: 'Z'}, 'PersonId': { 
           0: 14, 1: 14, 2: 14, 3: 14, 4: 15}, 'Provider': { 
            0: 'A', 1: 'A', 2: 'A', 3: 'A', 4: 'B'}, 'Year': { 
             0: 2012, 1: 2012, 2: 2013, 3: 2013, 4: 2012}} 
testdf = pd.DataFrame(testdf) 
testdf 
grouper_keys = [ 
    'Employer', 
    'Year', 
    'Division', 
    'Name', 
    'EmployeeId', 
    'PersonId'] 

testdf2 = pd.pivot_table(data=testdf, 
           values='Amt1', 
           index=grouper_keys, 
           columns='Provider', 
           fill_value=None, 
           margins=False, 
           dropna=True, 
           aggfunc=('sum', 'count'), 
          ) 

print(testdf2) 

otrzymujemy:

enter image description here

teraz Mogę uzyskać tylko sum dla A lub B przy użyciu

testdf2.loc[:, slice(None, ('sum', 'A'))] 

co daje

enter image description here

Jak mogę uzyskać zarównosumicount tylko dla A lub B

Odpowiedz

4

można użyć:

idx = pd.IndexSlice 
df = testdf2.loc[:, idx[['sum', 'count'], 'A']] 
print (df) 
                sum count 
Provider            A  A 
Employer Year Division Name EmployeeId PersonId    
Z  2012 C  H 14   14  311.17 2.0 
             15   NaN NaN 
     2013 C  H 14   14  386.29 2.0 

Innym rozwiązaniem:

df = testdf2.loc[:, (slice('sum','count'), ['A'])] 
print (df) 
                sum count 
Provider            A  A 
Employer Year Division Name EmployeeId PersonId    
Z  2012 C  H 14   14  311.17 2.0 
             15   NaN NaN 
     2013 C  H 14   14  386.29 2.0 
+0

Myślę, że OP chciał 'sum' i' count', a nie 'A' i' B'. – IanS

+0

@IanS - dziękuję. – jezrael

+0

dobrze, wystarczająco blisko;) '' testdf2.loc [:, idx [['suma', 'count'], ['A']]]] 'wygląda na 10 minut oczekiwania na odpowiedź – muon

4

Zastosowanie xs o przekroju

testdf2.xs('A', axis=1, level=1) 

enter image description here

lub utrzymania poziomu kolumnowej z drop_level=False

testdf2.xs('A', axis=1, level=1, drop_level=False) 

enter image description here