Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print(tabulate(...)) to pretty print multiIndex pandas ?

Say i have a pandas data frame:

import numpy as np
import pandas as pd
from tabulate import tabulate

A = pd.DataFrame(np.random.randint(0,10,(3,6)), index= ['uno', 'dos', 'tres'])
A.columns = ['A','B','C','D','E','F']
A.index.names = ['type']
A.columns.names= ['group']

h = [A.index.names[0] +'/'+ A.columns.names[0]] + list(A.columns)
print(tabulate(A, headers= h, tablefmt= 'grid'))

which gives:

+--------------+-----+-----+-----+-----+-----+-----+
| type/group   |   A |   B |   C |   D |   E |   F |
+==============+=====+=====+=====+=====+=====+=====+
| uno          |   3 |   1 |   6 |   0 |   7 |   0 |
+--------------+-----+-----+-----+-----+-----+-----+
| dos          |   9 |   5 |   3 |   0 |   6 |   6 |
+--------------+-----+-----+-----+-----+-----+-----+
| tres         |   6 |   7 |   4 |   6 |   8 |   4 |
+--------------+-----+-----+-----+-----+-----+-----+

Now adding a layer:

iterable = [['A', 'B'], ['AA', 'BB', 'CC']]
A.columns = pd.MultiIndex.from_product(iterable,
                                       names= ['group', 'subgroup'])
A.index.names = ['type']

will give using a print statement:

group     A        B      
subgroup AA BB CC AA BB CC
type                      
uno       3  1  6  0  7  0
dos       9  5  3  0  6  6
tres      6  7  4  6  8  4

Typically, this does not present well on a document such as pweave.

How can i use print(tabulate(...)) such as i have each group and sub group to shown on separate line ?

Thanks

like image 246
Cy Bu Avatar asked Jan 11 '17 14:01

Cy Bu


People also ask

How do you print a pretty data frame?

You can pretty print pandas dataframe using pd. set_option('display. max_columns', None) statement. Usecase: Your dataframe may contain many columns and when you print it normally, you'll only see few columns.

What is a MultiIndex in pandas?

The MultiIndex object is the hierarchical analogue of the standard Index object which typically stores the axis labels in pandas objects. You can think of MultiIndex as an array of tuples where each tuple is unique. A MultiIndex can be created from a list of arrays (using MultiIndex.


1 Answers

With tabulate version 0.8.1 or newer,

import numpy as np
import pandas as pd
from tabulate import tabulate

A = pd.DataFrame(np.random.randint(0,10,(3,6)), index= ['uno', 'dos', 'tres'])
A.columns = ['A','B','C','D','E','F']
A.index.names = ['type']
A.columns.names= ['group']

iterable = [['A', 'B'], ['AA', 'BB', 'CC']]
A.columns = pd.MultiIndex.from_product(iterable, names= ['group', 'subgroup'])

h = [A.index.names[0] +'/'+ A.columns.names[0]] + list(map('\n'.join, A.columns.tolist()))
print(tabulate(A, headers= h, tablefmt= 'grid'))

yields

+--------------+------+------+------+------+------+------+
| type/group   |    A |    A |    A |    B |    B |    B |
|              |   AA |   BB |   CC |   AA |   BB |   CC |
+==============+======+======+======+======+======+======+
| uno          |    8 |    4 |    3 |    3 |    4 |    2 |
+--------------+------+------+------+------+------+------+
| dos          |    1 |    1 |    1 |    9 |    8 |    9 |
+--------------+------+------+------+------+------+------+
| tres         |    0 |    4 |    3 |    6 |    1 |    7 |
+--------------+------+------+------+------+------+------+
like image 74
unutbu Avatar answered Oct 26 '22 23:10

unutbu