I have created a pandas dataframe as follows:
import pandas as pd
import numpy as np
ds = {'col1' : [1,1,2,3,4], 'col2' : [1,1,3,4,5], 'col3': [3,3,3,3,3]}
df = pd.DataFrame(data=ds)
The dataframe looks like this:
print(df)
col1 col2 col3
0 1 1 3
1 1 1 3
2 2 3 3
3 3 4 3
4 4 5 3
I have then produced two crosstabs as follows:
x1 = pd.crosstab(df['col1'], df['col2'], normalize='index').sort_index()
x2 = pd.crosstab(df['col1'], df['col3'], normalize='index').sort_index()
Which look like this:
print(x1)
print("")
print(x2)
col2 1 3 4 5
col1
1 1.0 0.0 0.0 0.0
2 0.0 1.0 0.0 0.0
3 0.0 0.0 1.0 0.0
4 0.0 0.0 0.0 1.0
col3 3
col1
1 1.0
2 1.0
3 1.0
4 1.0
I need to export those crosstabs results into one html file and that html file needs to have a table of contents at the top of the page containing the second element of the crosstab. So, the html would look something like this:
1. col2
2. col3
col2 1 3 4 5
col1
1 1.0 0.0 0.0 0.0
2 0.0 1.0 0.0 0.0
3 0.0 0.0 1.0 0.0
4 0.0 0.0 0.0 1.0
col3 3
col1
1 1.0
2 1.0
3 1.0
4 1.0