2016-03-20 14 views
2

Verwendung habe ich dieses Beispiel:verschiedene Aggregatfunktionen anwenden, wenn pivot_table

import pandas as pd 
import numpy as np 
dic = {'name': 
     ['j','c','q','j','c','q','j','c','q'], 
     'foo or bar':['foo','bar','bar','bar','foo','foo','bar','foo','foo'], 
     'amount':[10,20,30, 20,30,40, 200,300,400]} 
x = pd.DataFrame(dic) 
x 
pd.pivot_table(x, 
       values='amount', 
       index='name', 
       columns='foo or bar', 
       aggfunc=[np.mean, np.sum]) 

Es ist diese zurück:

enter image description here

Ich mag würde nur die markierten Spalten müssen. Warum kann ich Tupel im Aggfunc-Argument nicht so spezifizieren?

pd.pivot_table(x, 
       values='amount', 
       index='name', 
       columns='foo or bar', 
       aggfunc=[(np.mean, 'bar'), (np.sum, 'foo')]) 

Nutzen .ix wie hier (define aggfunc for each values column in pandas pivot table) die einzige Option?

+1

diese Frage bezogen werden: http://stackoverflow.com/questions/20119414/define-aggfunc-for-each -Werte-Spalte-in-Pandas-Pivot-Tabelle/20120225 # 20120225 – whytheq

Antwort

2

Ich glaube, Sie nicht Tupel für die aggfunc Parameter angeben können, aber man kann so etwas tun:

In [259]: p = pd.pivot_table(x, 
    .....:    values='amount', 
    .....:    index='name', 
    .....:    columns='foo or bar', 
    .....:    aggfunc=[np.mean, np.sum]) 

In [260]: p 
Out[260]: 
      mean  sum 
foo or bar bar foo bar foo 
name 
c   20 165 20 330 
j   110 10 220 10 
q   30 220 30 440 

In [261]: p.columns = ['{0[0]}_{0[1]}'.format(col) if col[1] else col[0] for col in p.columns.tolist()] 

In [262]: p.columns 
Out[262]: Index(['mean_bar', 'mean_foo', 'sum_bar', 'sum_foo'], dtype='object') 

In [264]: p[['mean_bar','sum_foo']] 
Out[264]: 
     mean_bar sum_foo 
name 
c   20  330 
j   110  10 
q   30  440 
2

Um in der Lage sein zu tun, dass, wie in der Antwort, die Sie, sofern Sie entsprechende Spalten erstellen müssen dafür. Sie könnten das tun, mit:

x['foo'] = x.loc[x['foo or bar'] == 'foo', 'amount'] 
x['bar'] = x.loc[x['foo or bar'] == 'bar', 'amount'] 

In [81]: x 
Out[81]: 
    amount foo or bar name foo bar 
0  10  foo j 10.0 NaN 
1  20  bar c NaN 20.0 
2  30  bar q NaN 30.0 
3  20  bar j NaN 20.0 
4  30  foo c 30.0 NaN 
5  40  foo q 40.0 NaN 
6  200  bar j NaN 200.0 
7  300  foo c 300.0 NaN 
8  400  foo q 400.0 NaN 

Und dann könnten Sie folgendes:

In [82]: x.pivot_table(values=['foo','bar'], index='name', aggfunc={'bar':np.mean, 'foo':sum}) 
Out[82]: 
     bar foo 
name    
c  20.0 330.0 
j  110.0 10.0 
q  30.0 440.0 
Verwandte Themen