Python Pandas 防止单元格换行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38746112/
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
Python Pandas prevent line break in cell
提问by Zelphir Kaltstahl
Using the following functions, I already managed to prevent truncation in the output in my notebook:
使用以下功能,我已经设法防止笔记本中的输出被截断:
pd.set_option('display.max_colwidth', 200)
pd.set_option('display.max_columns', 0)
However, it still breaks long lines within some cells (not truncates!). How can I prevent it from doing that?
但是,它仍然会在某些单元格中断开长行(不是截断!)。我怎样才能防止它这样做?
(I don't care how wide the total table will be, because I can simply scroll in the output of the notebook cell ...)
(我不在乎总表的宽度,因为我可以简单地滚动笔记本单元格的输出......)
回答by Zian
for me worked pandas.set_option('display.expand_frame_repr', False)
as suggested by @EdChum.
对我来说,pandas.set_option('display.expand_frame_repr', False)
按照@EdChum 的建议工作。
import pandas
matrix = [[None]*5]*4
matrix[1][1] = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut"
matrix[1][4] = matrix[1][1]
print pandas.DataFrame(matrix) # will print the matrix,
# column 4 is completely underneath the other 3 columns with all rows
pandas.set_option('display.expand_frame_repr', False)
print pandas.DataFrame(matrix) # prints only one table (no break of columns)
回答by Aze
This worked for me, I'm using python 2.7, and tested it with 3.6 on the Pycharm IDE.
这对我有用,我使用的是 python 2.7,并在 Pycharm IDE 上用 3.6 对其进行了测试。
try:
df = pd.read_excel(file,sheet_name="sheet", inplace=True, usecols="A,C:E")
with pd.option_context('display.max_rows', 300, 'display.max_columns', 7, 'display.expand_frame_repr', False):
print(df)
except:
print ('failed')
However, for the Jupyter Notebooks, I believe this is the syntax:
但是,对于 Jupyter Notebooks,我相信这是语法:
pd.options.display.max_rows
pd.set_option('display.max_colwidth', -1)
pd.DataFrame({'string_x': string_x}, index = [0])