Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Pandas prevent line break in cell

Tags:

python

pandas

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 ...)

like image 863
Zelphir Kaltstahl Avatar asked Aug 03 '16 14:08

Zelphir Kaltstahl


3 Answers

for me worked pandas.set_option('display.expand_frame_repr', False) as suggested by @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)
like image 87
Zian Avatar answered Sep 28 '22 22:09

Zian


This worked for me, I'm using python 2.7, and tested it with 3.6 on the Pycharm IDE.

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:

pd.options.display.max_rows

pd.set_option('display.max_colwidth', -1)

pd.DataFrame({'string_x': string_x}, index = [0])
like image 45
Aze Avatar answered Sep 25 '22 22:09

Aze


None of the solutions here worked in jupyter for me, what worked was appealing to CSS:

from IPython.display import HTML
HTML(f"<style>td{white-space: nowrap !important;}</style>")

I've not tested but this may work too:

df.style.set_table_styles([{'selector': 'td', 'props': 'white-space: nowrap !important;'}])
like image 1
RomuloPBenedetti Avatar answered Sep 27 '22 22:09

RomuloPBenedetti