简体   繁体   中英

Pivot Table Pandas

My problem is edit the subtotals of each category and display the total and then edit the column "All" but I can't do it . Please, tell me how to proceed please. Link file for prets_preview : http://www.cjoint.com/c/GBijO4uZYjt and issues_out : http://www.cjoint.com/c/GBsm0IXIILE Thanks you for your help.

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)

I need to display the subtotals more clearly and edit the column "All" but i don't know how to do.

                                        **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**

You can use pandas.melt function to reshape that dataframe with id_vars is localisation and value_vars are all variables in support

It might help pandas.melt

Simply use the margins_name argument of pandas.pivot_table to rename the default All 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 (using posted linked data)

                                       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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM