Jupyter笔记本电脑并排显示两个 pandas 桌 [英] Jupyter notebook display two pandas tables side by side

查看:181
本文介绍了Jupyter笔记本电脑并排显示两个 pandas 桌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个熊猫数据框,我想在Jupyter笔记本中显示它们.

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

做类似的事情:

display(df1)
display(df2)

将它们一个接一个地显示:

Shows them one below another:

我想在第一个数据框的右边添加第二个数据框.有一个类似的问题,但似乎有人对将它们合并到一个显示差异的数据框中感到满意他们之间.

I would like to have a second dataframe on the right of the first one. There is a similar question, but it looks like there a person is satisfied either with merging them in one dataframe of showing the difference between them.

这对我不起作用.在我的情况下,数据帧可以表示完全不同的(不可比较的元素),并且它们的大小可以不同.因此,我的主要目标是节省空间.

This will not work for me. In my case dataframes can represent completely different (non-comparable elements) and the size of them can be different. Thus my main goal is to save space.

推荐答案

您可以覆盖输出代码的CSS.默认情况下,它使用flex-direction: column.尝试将其更改为row.这是一个示例:

You could override the CSS of the output code. It uses flex-direction: column by default. Try changing it to row instead. Here's an example:

import pandas as pd
import numpy as np
from IPython.display import display, HTML

CSS = """
.output {
    flex-direction: row;
}
"""

HTML('<style>{}</style>'.format(CSS))

当然,您可以根据需要进一步自定义CSS.

You could, of course, customize the CSS further as you wish.

如果只希望定位一个单元格的输出,请尝试使用:nth-child()选择器.例如,以下代码将只修改笔记本中第5个单元格的输出的CSS:

If you wish to target only one cell's output, try using the :nth-child() selector. For example, this code will modify the CSS of the output of only the 5th cell in the notebook:

CSS = """
div.cell:nth-child(5) .output {
    flex-direction: row;
}
"""

这篇关于Jupyter笔记本电脑并排显示两个 pandas 桌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆