我可以在 pandas 中设置可变的列宽吗? [英] Can I set variable column widths in pandas?

查看:126
本文介绍了我可以在 pandas 中设置可变的列宽吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在pandas数据框中有几列带有长字符串的文本,但是我只想检查其中的一列.有没有一种方法可以使用pd.set_option('max_colwidth', 60)的内容,但仅用于单列,而不是扩展df中所有列的宽度?

I've got several columns with long strings of text in a pandas data frame, but am only interested in examining one of them. Is there a way to use something along the lines of pd.set_option('max_colwidth', 60) but for a single column only, rather than expanding the width of all the columns in my df?

推荐答案

如果要在Jupyter Notebook中更改显示,可以使用

If you want to change the display in a Jupyter Notebook, you can use the Style feature. To use this formatting for only some columns simply indicate the column(s) to enlarge thanks to the subset parameter. This is basically HTML and CSS.

### Test data
df = DataFrame({'text': ['foo foo foo foo foo foo foo foo', 'bar bar bar bar bar'],
                 'number': [1, 2]})

df.style.set_properties(subset=['text'], **{'width': '300px'})

这篇关于我可以在 pandas 中设置可变的列宽吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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