Home > Software design >  Print dataframe groups while printing
Print dataframe groups while printing

Time:06-24

I want to print expanded form of grouped dataframe.

I create dataframe by:

grouped_df = df.groupby(['A', 'B', 'C']).agg({'value': 'sum'})

This leads to printing dataframe like:

A   B    C  value
a1  b1  c1    12
        c2    10
    b2  c3    14
        c4    56
a2  b1  c1    22
        c2    50
    b2  c3    14
        c4    86
....

I want to print each row filled with values instead. E.g:

A   B    C  value
a1  b1  c1    12
a1  b1  c2    10
a1  b2  c3    14
a1  b2  c4    56
a2  b1  c1    22
a2  b1  c2    50
a2  b2  c3    14
a2  b2  c4    86
....

CodePudding user response:

df = pd.DataFrame()
df['A'] = ['a1', 'a1', 'a1', 'a1', 'a2', 'a2', 'a2', 'a2']
df['B'] = ['b1', 'b1', 'b2', 'b2', 'b1', 'b1', 'b2', 'b2']
df['C'] = ['c1', 'c2', 'c3', 'c4', 'c1', 'c2', 'c3', 'c4']
df['value'] = [12, 10, 14, 56, 22, 50, 14, 85]
grouped_df = df.groupby(['A', 'B', 'C']).aggregate({'value': 'sum'})
grouped_df
          value
A  B  C        
a1 b1 c1     12
      c2     10
   b2 c3     14
      c4     56
a2 b1 c1     22
      c2     50
   b2 c3     14
      c4     85
grouped_df.reset_index()  # Do reset_index
    A   B   C  value
0  a1  b1  c1     12
1  a1  b1  c2     10
2  a1  b2  c3     14
3  a1  b2  c4     56
4  a2  b1  c1     22
5  a2  b1  c2     50
6  a2  b2  c3     14
7  a2  b2  c4     85

As G.Anderson's comment, as_index=False gives same result with reset_index

  • Related