iT邦幫忙

3

[Python] Pandas 並列(同時)顯示DataFrame

平常在使用Jupyter notebook時,一個cell 只能顯示一個DataFrames
利用自製Class 可以同時顯示多個DataFrames

https://ithelp.ithome.com.tw/upload/images/20190225/20115086Up11liMwLq.png

class display(object):
    """Display HTML representation of multiple objects"""
    template = """<div style="float: left; padding: 10px;">
    <p style='font-family:"Courier New", Courier, monospace'>{0}</p>{1}
    </div>"""
    def __init__(self, *args):
        self.args = args
        
    def _repr_html_(self):
        return '\n'.join(self.template.format(a, eval(a)._repr_html_())
                         for a in self.args)
    
    def __repr__(self):
        return '\n\n'.join(a + '\n' + repr(eval(a))
                           for a in self.args)
d = {'col1': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)

df2 = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),columns=['a', 'b', 'c'])

display('df', 'df2')

尚未有邦友留言

立即登入留言