Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas DataFrames: how to wrap text with no whitespace

Tags:

I'm viewing a Pandas DataFrame in a Jupyter Notebook, and my DataFrame contains URL request strings that can be hundreds of characters long without any whitespace separating characters.

Pandas seems to only wrap text in a cell when there's whitespace, as shown on the attached picture:

enter image description here

If there isn't whitespace, the string is displayed in a single line, and if there isn't enough space my options are either to see a '...' or I have to set display.max_colwidth to a huge number and now I have a hard-to-read table with a lot of scrolling.

Is there a way to force Pandas to wrap text, say, every 100 characters, regardless of whether there is whitespace?

like image 328
user1956609 Avatar asked Dec 20 '15 00:12

user1956609


People also ask

How do you get rid of white space in pandas?

lstrip() is used to remove spaces from the left side of string, str. rstrip() to remove spaces from right side of the string and str. strip() removes spaces from both sides. Since these are pandas function with same name as Python's default functions, .

How do you wrap text in pandas?

Pandas str. wrap() is an important method when dealing with long text data (Paragraphs or messages). This is used to distribute long text data into new lines or handle tab spaces when it exceeds the passed width. Since this is a string method, .

What is flatten Dataframe?

In this article, we are going to see how to flatten a list of DataFrames. Flattening is defined as Converting or Changing data format to a narrow format. The advantage of the flattened list is Increases the computing speed and Good understanding of data.


2 Answers

You can set

import pandas as pd pd.set_option('display.max_colwidth', 0) 

and then each column will be just as big as it needs to be in order to fully display it's content. It will not wrap the text content of the cells though (unless they contain spaces).

like image 192
paulo.filip3 Avatar answered Sep 26 '22 11:09

paulo.filip3


You can use str.wrap method:

df['user_agent'] = df['user_agent'].str.wrap(100) #to set max line width of 100 
like image 23
O.Suleiman Avatar answered Sep 24 '22 11:09

O.Suleiman