Jupyter notebook display two pandas tables side by side

后端 未结 12 1942
迷失自我
迷失自我 2020-12-04 04:56

I have two pandas dataframes and I would like to display them in Jupyter notebook.

Doing something like:

display(df1)
display(df2)

12条回答
  •  生来不讨喜
    2020-12-04 05:58

    @zarak code is pretty small but affects the layout of the whole notebook. Other options are a bit messy for me.

    I've added some clear CSS to this answer affecting only current cell output. Also you are able to add anything below or above dataframes.

    from ipywidgets import widgets, Layout
    from IPython import display
    import pandas as pd
    import numpy as np
    
    # sample data
    df1 = pd.DataFrame(np.random.randn(8, 3))
    df2 = pd.DataFrame(np.random.randn(8, 3))
    
    # create output widgets
    widget1 = widgets.Output()
    widget2 = widgets.Output()
    
    # render in output widgets
    with widget1:
        display.display(df1.style.set_caption('First dataframe'))
        df1.info()
    with widget2:
        display.display(df2.style.set_caption('Second dataframe'))
        df1.info()
    
    
    # add some CSS styles to distribute free space
    box_layout = Layout(display='flex',
                        flex_flow='row',
                        justify_content='space-around',
                        width='auto'
                       )
        
    # create Horisontal Box container
    hbox = widgets.HBox([widget1, widget2], layout=box_layout)
    
    # render hbox
    hbox
    

提交回复
热议问题