Display Pandas DataFrames Side by Side¶
import pandas as pd
import numpy as np
from IPython.display import display_html
HTML Function for Displaying Side by Side¶
def display_side_by_side(*args):
html_str = ''
for df in args:
html_str += df.to_html()
display_html(
html_str.replace('table','table style="display:inline"'),
raw=True
)
Create Test DataFrames¶
df1 = pd.DataFrame({
'date': pd.date_range('2021-01-01', '2021-01-07'),
'sales': np.random.rand(7)*1000
})
display(df1)
df2 = pd.DataFrame({
'date': pd.date_range('2021-02-01', '2021-02-07'),
'sales': np.random.rand(7)*1000
})
display(df2)
Display Side by Side¶
display_side_by_side(df1, df2)