2017-02-18 3 views
0

Mein Problem ist, die Teilergebnisse der einzelnen Kategorien bearbeiten und die Gesamt anzuzeigen, und dann die Spalte „Alle“ bearbeiten, aber ich kann es nicht tun. Bitte sagen Sie mir, wie es weiter geht. Link-Datei für prets_preview: http://www.cjoint.com/c/GBijO4uZYjt und issues_out: http://www.cjoint.com/c/GBsm0IXIILE Vielen Dank für Ihre Hilfe.Pivot-Tabelle Pandas

In [1]: import pandas as pd 

In [2]: import numpy as np 

In [3]: from pandas import DataFrame 

In [4]: issues=pd.read_table('prets_preview.csv') 

In [5]: site={'BUS1A' : 'Zèbre', 'MED0A' : 'Collectivités'} 

In [6]: issues ['localisation'] = issues['localisation'].map(site) 

In [7]: issues ['localisation'] = issues['localisation'].fillna('Médiathèque') 

In [8]: resultat = issues ['id_exemplaire'].groupby([issues['localisation'],issues.ccode, issues['support']]).count() 

In [9]: table = pd.pivot_table(issues,values=['id_exemplaire'], index=['locali sation'], columns =['support'], aggfunc =np.sum,margins = True) 

In [10]: resultat = table.stack('support') 

In [11]: resultat.to_csv('issues_out.csv') 

In [12]: resultat = issues['id_exemplaire'].groupby(issues['localisation']) 
    .count() 

In [13]: resultat = issues['id_exemplaire'].groupby([issues['localisation'], is ues['support'], issues['ccode']]).count() 


In [16]: table = pd.pivot_table (issues, values=['id_exemplaire'], index=['localisation'], columns=['support'], aggfunc= np.sum, margins = True) 

Ich brauche mehr eindeutig die Teilergebnisse anzuzeigen und die Spalte „Alle“ bearbeiten, aber ich weiß nicht, wie zu tun ist.

         **id_exemplaire 
localisation support 
Collectivités All       300390.0 
       DVD         0.0 
       Disque compact      0.0 
       Disque microsillon     0.0 
       Livre       300390.0 
       Livre en gros caractères   0.0 
       Livre sonore      0.0 
       Périodique       0.0 
Médiathèque All       23610694.0 
       DVD       3710341.0 
       Disque compact    1684356.0 
       Disque microsillon    338976.0 
       Livre      15731162.0 
       Livre en gros caractères  514064.0 
       Livre sonore     595185.0 
       Périodique     1036610.0 
Zèbre   All       800167.0 
       DVD       192799.0 
       Disque compact      0.0 
       Disque microsillon     0.0 
       Livre       607368.0 
       Livre en gros caractères   0.0 
       Livre sonore      0.0 
       Périodique       0.0 
All   All       24711251.0 
       DVD       3903140.0 
       Disque compact    1684356.0 
       Disque microsillon    338976.0 
       Livre      16638920.0 
       Livre en gros caractères  514064.0 
       Livre sonore     595185.0 
       Périodique     1036610.0** 
+0

* I die Teilergebnisse deutlicher angezeigt werden müssen und bearbeiten Sie die "All" Spalte * ... zeigen Sie uns gewünschte Ergebnis als Dies kann auf viele Arten interpretiert werden. – Parfait

+0

Ich möchte, dass die Spalte "ALL" umbenannt wird. Zum Beispiel: Alle -> Total Ich bin Französisch und ich muss diese Spalte bitte umbenennen. –

Antwort

0

Sie pandas.melt Funktion, den Datenrahmen neu zu gestalten können mit id_varslocalisation und value_vars sind alle Variablen in support

Es pandas.melt

0

einfach auf das margins_name Argument von pandas.pivot_table verwenden könnte helfen benennen Sie den Standard Alle Label.

table = pd.pivot_table(issues, 
         values = ['id_exemplaire'], 
         index = ['localisation'], 
         columns = ['support'], 
         aggfunc = np.sum, 
         margins = True, 
         margins_name = 'Total') 

resultat = table.stack('support') 
print(resultat) 

Output(geschrieben verknüpften Daten verwenden)

         id_exemplaire 
localisation support         
BUS1A  DVD       192799.0 
      Livre       607368.0 
      Total       800167.0 
MED0A  Livre       300390.0 
      Total       300390.0 
MED1A  DVD       3710341.0 
      Livre       8242130.0 
      Livre en gros caractères  514064.0 
      Périodique      862281.0 
      Total      13328816.0 
MED2A  Livre       7489032.0 
      Livre sonore     595185.0 
      Périodique      174329.0 
      Total       8258546.0 
MED3A  Disque compact    1462267.0 
      Disque microsillon    338976.0 
      Total       1801243.0 
MED3C  Disque compact     222089.0 
      Total       222089.0 
Total  DVD       3903140.0 
      Disque compact    1684356.0 
      Disque microsillon    338976.0 
      Livre      16638920.0 
      Livre en gros caractères  514064.0 
      Livre sonore     595185.0 
      Périodique     1036610.0 
      Total      24711251.0