1
votes

Pandas pivot_table: impossible d'obtenir le format correct

J'ai les données suivantes comme configuration:

    df = pd.pivot_table(
            df,
            index=['index_1', 'index_2'],
            columns=['month'],
            #   The 2 following lines are implicit, and don't change the output.
            #   values=['value_1', 'value_2'],
            #   aggfunc='sum'
    )
    print(df)
    #                   value_1      value_2      
    # month             feb  jan     feb   jan
    # index_1 index_2                           
    # A       A         NaN  0.0     NaN   8.0
    #         B         NaN  1.0     NaN   9.0
    #         C         2.0  NaN    10.0   NaN
    #         D         3.0  NaN    11.0   NaN
    # B       A         NaN  4.0     NaN  12.0
    #         B         NaN  5.0     NaN  13.0
    #         C         6.0  NaN    14.0   NaN
    #         D         7.0  NaN    15.0   NaN

Ma sortie attendue ressemblerait à ceci ... (je l'ai fait à la main)

    print(expected_output)
    #                   jan             feb      
    # month             value_1 value_2 value_1 value_2
    # index_1 index_2                           
    # A       A         0.0     8.0     NaN     NaN
    #         B         1.0     9.0     NaN     NaN
    #         C         NaN     NaN     2.0     10.0
    #         D         NaN     NaN     3.0     11.0
    # B       A         4.0     12.0    NaN     NaN
    #         B         5.0     13.0    NaN     NaN
    #         C         NaN     NaN     6.0     14.0
    #         D         NaN     NaN     7.0     15.0


0 commentaires

3 Réponses :


2
votes

Utilisez Catégoriel ordonné pour un tri correct des mois avec DataFrame.swaplevel et DataFrame.sort_index :

months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aAug', 'sep', 'oct', 'nov', 'dec']
df['month'] = pd.Categorical(df['month'], categories=months, ordered=True)

df = (df.set_index(['index_1','index_2','month'])
        .unstack()
        .swaplevel(0,1, axis=1)
        .sort_index(axis=1))
print (df)
month               jan             feb        
                value_1 value_2 value_1 value_2
index_1 index_2                                
A       A           0.0     8.0     NaN     NaN
        B           1.0     9.0     NaN     NaN
        C           NaN     NaN     2.0    10.0
        D           NaN     NaN     3.0    11.0
B       A           4.0    12.0     NaN     NaN
        B           5.0    13.0     NaN     NaN
        C           NaN     NaN     6.0    14.0
        D           NaN     NaN     7.0    15.0

Ou:

months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aAug', 'sep', 'oct', 'nov', 'dec']
df['month'] = pd.Categorical(df['month'], categories=months, ordered=True)

df = pd.pivot_table(
            df,
            index=['index_1', 'index_2'],
            columns=['month'],
    ).swaplevel(0,1, axis=1).sort_index(axis=1)
print(df)
month               jan             feb        
                value_1 value_2 value_1 value_2
index_1 index_2                                
A       A           0.0     8.0     NaN     NaN
        B           1.0     9.0     NaN     NaN
        C           NaN     NaN     2.0    10.0
        D           NaN     NaN     3.0    11.0
B       A           4.0    12.0     NaN     NaN
        B           5.0    13.0     NaN     NaN
        C           NaN     NaN     6.0    14.0
        D           NaN     NaN     7.0    15.0


0 commentaires

1
votes

que diriez-vous de stack et unstack :

month               jan             feb        
                value_1 value_2 value_1 value_2
index_1 index_2                                
A       A           0.0     8.0     NaN     NaN
        B           1.0     9.0     NaN     NaN
        C           NaN     NaN     2.0    10.0
        D           NaN     NaN     3.0    11.0
B       A           4.0    12.0     NaN     NaN
        B           5.0    13.0     NaN     NaN
        C           NaN     NaN     6.0    14.0
        D           NaN     NaN     7.0    15.0

df.set_index(['index_1','index_2','month']).stack().unstack([2,3])


0 commentaires

0
votes
import pandas as pd
df = pd.DataFrame({
'index_1' : ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
'index_2' : ['A', 'B', 'C', 'D', 'A', 'B', 'C', 'D'],
'month' : ['jan', 'jan', 'feb', 'feb', 'jan', 'jan', 'feb', 'feb'],
'value_1' : range(0, 8),
'value_2' : range(8, 16)})

print(df)
df = df.set_index(['index_1','index_2','month']).unstack(level=-1)
print(df)
Instead of a pivot table this is something called a hierarchical index.  

0 commentaires