1

I have a dataframe imported from csv file and I have set option of ('display.max_colwidth', None) still the column (column named 'Week') values (which are strings) come in two lines. What should I write so that it comes in one line?

enter image description here

One thing is that if I display less columns then it comes one line (in the above pic I had set the option to display all columns, although they are not visible in the truncated screenshot). This can be seen in below pic.

enter image description here

Dhruv
  • 117
  • 11

2 Answers2

1

pd.set_option sets one single option at the time. If you want to set multiple options you have to call it multiple times.

In your case that would be:

pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.max_colwidth', None)

Alternatively you can set all these options using a for loop:

options = {'display.max_rows': None, 
           'display.max_columns': None, 
           'display.max_colwidth', None}

for option, value in options.items():
    pd.set_option(option, value)
mozway
  • 194,879
  • 13
  • 39
  • 75
Kastakin
  • 121
  • 1
  • 9
  • 1
    don't use a list comprehension for side effects, use a classical loop: `for option, value in options.items(): pd.set_option(option, value)` – mozway Apr 22 '22 at 14:11
  • I didn't know that could cause any issues, thanks for poiting it out! I'm going to fix my answer. – Kastakin Apr 22 '22 at 14:13
  • 1
    It's just bad practice, this creates a list of `[None, None, None]` uselessly ([more on this in this Q/A](https://stackoverflow.com/questions/5753597/is-it-pythonic-to-use-list-comprehensions-for-just-side-effects)). You can use it for code golf ;) – mozway Apr 22 '22 at 14:14
  • btw, no need to credit me ;) – mozway Apr 22 '22 at 14:17
  • It's still not working. Actually I had tried this before and it was not working so I just put all the statements into one. I've updated the screenshot, you can look into that – Dhruv Apr 23 '22 at 03:30
  • Also if I display less no. of columns then the display is fine, (I've updated in the question). So, I think there must be some other option to set. – Dhruv Apr 23 '22 at 03:40
0

What worked for me was this answer. It modifies the CSS of the style option of dataframe.

I'm putting the code for a quick reference here or if the link gets corrupted in future.

# @RomuloPBenedetti's answer, check out the link
df.style.set_table_styles([{'selector': 'td', 'props': 'white-space: nowrap !important;'}])
Dhruv
  • 117
  • 11