停止 jupyter notebook 在 Pandas html 表输出中包装单元格内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37716442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stop jupyter notebook wrapping cell contents in pandas html table output
提问by Mike
The pandas option max_colwidth
controls how many characters will be included in the repr of a dataframe:
pandas 选项max_colwidth
控制数据帧的 repr 中将包含多少个字符:
import string, random
import pandas as pd
df = pd.DataFrame([''.join(random.choice(string.ascii_lowercase + ' ') for j in range(1000)) for i in range(4)])
pd.options.display.max_colwidth = 10
print(df)
yields
产量
0
0 lmftge...
1 pqttqb...
2 wi wgy...
3 ow dip...
and
和
pd.options.display.max_colwidth = 30
print(df)
yields
产量
0
0 lmftgenioerszvgzfaxorzciow...
1 pqttqbqqe pykgguxnjsspbcti...
2 wi wgybtgcbxkobrwnaxpxwsjc...
3 ow dippaiamvvcofvousieckko...
And you can set pd.options.display.max_colwidth = 0
to remove the limit altogether. Fine so far!
您可以设置pd.options.display.max_colwidth = 0
完全删除限制。到目前为止还好!
But if the dataframe is rendered in HTML inside a notebook, the notebook will wrap the table of the column to the width of the display, regardless of this setting:
但是,如果数据帧在笔记本内以 HTML 格式呈现,则无论此设置如何,笔记本都会将列的表格包装到显示的宽度:
Is there any way to avoid this, i.e. to have the HTML table column rendered as wide as is necessary to fit the each row on a single line?
有什么办法可以避免这种情况,即让 HTML 表格列呈现尽可能宽的宽度以将每一行放在一行上?
More generally, is it possible to control the width of HTML table columns in notebook output independent of the number of characters in the pandas output?
更一般地说,是否可以独立于 Pandas 输出中的字符数来控制笔记本输出中 HTML 表格列的宽度?
采纳答案by Ben
If you make a file: ~/.jupyter/custom$ atom custom.css
and then put this in it:
如果你制作一个文件:~/.jupyter/custom$ atom custom.css
然后把它放进去:
.dataframe td {
white-space: nowrap;
}
Then it will force the cell to show as one line, but then you get a scrolling table.
然后它会强制单元格显示为一行,但是你会得到一个滚动表。
If you want it to not scroll, then set:
如果您希望它不滚动,请设置:
div.output_subarea {
overflow-x: inherit;
}
and then it'll be as wide as it needs to be:
然后它会尽可能宽:
It's not super pretty, but I'm sure that you can tidy it up if needs be.
它不是特别漂亮,但我相信如果需要,你可以把它整理好。
I found thisvery helpful. You'll also need to restart the notebook after you first create the css file for it to register, but from then on you can just refresh the page to see the changes to the css take effect.
我发现这很有帮助。在首先创建要注册的 css 文件后,您还需要重新启动笔记本,但从那时起,您只需刷新页面即可查看对 css 所做的更改是否生效。
回答by SnowFrogger
Building on Ben's answer, but without needing to go into the custom css files, which work differently for juptyter lab.
基于 Ben 的回答,但无需进入自定义 css 文件,这对于 juptyter 实验室的工作方式有所不同。
Just put this in a cell and run it:
只需将它放在一个单元格中并运行它:
%%html
<style>
.dataframe td {
white-space: nowrap;
}
</style>