Home > Software design >  Displaying multiple dataframes together in formation
Displaying multiple dataframes together in formation

Time:08-12

So I have a very large population divided into three equal groups: A, B and C.

I can easily create a dataframe showing how members of each group interact with each other. For example, this can be done without a problem:

Dataframe 1:
               [columns for A]
[rows for A]

Dataframe 2:
               [columns for B]
[rows for A]
...

and so on.

What I am asking is whether it is possible to display all 9 similar dataframes at once, in a 3x3 formation:

[Dataframe 1][Dataframe 2][Dataframe 3]
[Dataframe 4][Dataframe 5][Dataframe 6]
[Dataframe 7][Dataframe 8][Dataframe 9]

Is there some sort of method that allows me to do this? You can assume that there is already a createdf method for each individual Dataframe.

CodePudding user response:

I found a function that someone else wrote , that might help:

from IPython.display import display_html
from itertools import chain,cycle
def display(*args,titles=cycle([''])):
  html_str=''
  for df,title in zip(args, chain(titles,cycle(['</br>'])) ):
     html_str ='<th style="text-align:center"><td style="vertical-align:top">'
     html_str =f'<h2 style="text-align: center;">{title}</h2>'
     html_str =df.to_html().replace('table','table style="display:inline"')
     html_str ='</td></th>'
  display_html(html_str,raw=True)

#Example_data:

df1 = pd.DataFrame(np.arange(12).reshape((3,4)),columns=['A','B','C','D',])
df2 = pd.DataFrame(np.arange(16).reshape((4,4)),columns=['A','B','C','D',])
display(df1,df2,df1, titles=['one','two', 'three']) 

CodePudding user response:

Apparently the answer is something like this:

def display_in_matrix_form(*args, titles = cycle(['']), rows, columns):
    for i in range(0, rows):
        html_str=''
        row_args = args[i*columns:(i 1)*columns]
        row_titles = titles[i*columns:(i 1)*columns]
        for j in range(0, columns):
            html_str ='<th style="text-align:center"><td style="vertical-align:top">'
            html_str =f'<h2 style="text-align: center;">{row_titles[j]}</h2>'
            html_str =row_args[j].to_html().replace('table','table style="display:inline"')
            html_str ='</td></th>'
        display_html(html_str,raw=True)

display_in_matrix_form(df1,df2,df1,df2, titles=['one','two', 'three', 'four'], rows = 2, columns = 2) 
  • Related