I have referred to the question Jupyter notebook display two pandas tables side by side. But the solution doesn't seem to work for me, i.e., the first answer on the page.
from IPython.display import display_html
from itertools import chain,cycle
def display_side_by_side(*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)
I can't get titles right. So it must be a problem with this line but I don't understand HTML at all so I can't fix it:
html_str+=f'<h2 style="text-align: center;">{title}</h2>'
Running this with the above line commented out:
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',])
df3 = pd.DataFrame(np.arange(12).reshape((3,4)),columns=['A','B','C','D',])
display_side_by_side(df1,df2,df3, titles=['Foo','Foo Bar','Foo Bar Bar'])
Gives me three data frames printed side by side.
But running the full function has the title at the centre of the page instead of the centre of the data frame. Therefore, this prints three data frames in three different lines with the data frames aligned leftwards and the titles in the centre.
I'm sorry this might seem extremely simply but I don't have the Karma to comment on the referred post so I had to create a question.
html_str output:
With the line html_str+=f'<h2 style="text-align: center;">{title}</h2>'
commented out:
<th style="text-align:center"><td style="vertical-align:top"><table style="display:inline" border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<th>1</th>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
</tr>
<tr>
<th>2</th>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
</tr>
</tbody>
</table style="display:inline"></td></th><th style="text-align:center"><td style="vertical-align:top"><table style="display:inline" border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<th>1</th>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
</tr>
<tr>
<th>2</th>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
</tr>
<tr>
<th>3</th>
<td>12</td>
<td>13</td>
<td>14</td>
<td>15</td>
</tr>
</tbody>
</table style="display:inline"></td></th><th style="text-align:center"><td style="vertical-align:top"><table style="display:inline" border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<th>1</th>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
</tr>
<tr>
<th>2</th>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
</tr>
</tbody>
</table style="display:inline"></td></th>
Without the line commented out:
<h2 style="text-align: center;">Foo</h2><th style="text-align:center"><td style="vertical-align:top"><table style="display:inline" border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<th>1</th>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
</tr>
<tr>
<th>2</th>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
</tr>
</tbody>
</table style="display:inline"></td></th><h2 style="text-align: center;">Foo Bar</h2><th style="text-align:center"><td style="vertical-align:top"><table style="display:inline" border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<th>1</th>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
</tr>
<tr>
<th>2</th>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
</tr>
<tr>
<th>3</th>
<td>12</td>
<td>13</td>
<td>14</td>
<td>15</td>
</tr>
</tbody>
</table style="display:inline"></td></th><h2 style="text-align: center;">Foo Bar Bar</h2><th style="text-align:center"><td style="vertical-align:top"><table style="display:inline" border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<th>1</th>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
</tr>
<tr>
<th>2</th>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
</tr>
</tbody>
</table style="display:inline"></td></th>
{title}
'` *first* and only *then* `html_str+='{title}
'` \ `html_str+='{title}
'`` and only in the second line, you have ``. Yet this `` comes from this snippet `"` will morph into ``), and in the correct order as shown above this snippet comes *first* in the loop that concatenates the string, and in the resulting html this makes `
– ouroboros1 Sep 29 '22 at 23:32` a properly nested element *inside* each table. So, how could the string possibly end up reversing these two elements? Seems impossible to me.
{title}
'`. And instead of the line `html_str+=df.to_html().replace('table','table style="display:inline"')` write: `html_str+=df.to_html().replace('table border="1" class="dataframe">',f'table style="display:inline" border="1" class="dataframe">{title}
')`. This also works for me. If you keep getting the same awkward result, maybe your version of `IPython.display` has some oddity in the function `display_html`. – ouroboros1 Sep 29 '22 at 23:49