user15851936
user15851936

Reputation:

How to display the full-text of a column in Pandas

I have a data frame that contains a column with long texts.

To demonstrate how it looks (note the ellipses "..." where text should continue):

id  text                       group 
123 My name is Benji and I ... 2

The above text is actually longer than that phrase. For example it could be:

My name is Benji and I am living in Kansas.

The actual text is much longer than this.

When I try to subset the text column only, it only shows the partial text with the dots "...".

I need to make sure full text is shown for text sumarization later. But I'm not sure how to show the full text when selecting the text column.

My df['text'] output looks something like this:

1    My name is Benji and I ... 
2    He went to the creek and ... 

How do I show the full text and without the index number?

Upvotes: 7

Views: 23364

Answers (3)

devReddit
devReddit

Reputation: 2947

You can use pd.set_option with display.max_colwidth to display automatic line-breaks and multi-line cells:

display.max_colwidth : int or None

The maximum width in characters of a column in the repr of a pandas data structure. When the column overflows, a “…” placeholder is embedded in the output. A ‘None’ value means unlimited. [default: 50]

So in your case:

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

For older versions, like version 0.22, use -1 instead of None

Upvotes: 16

santosh kumar
santosh kumar

Reputation: 1

dataframe.head(1)['columnname'].values

Upvotes: 0

cosmic_inquiry
cosmic_inquiry

Reputation: 2684

You can convert to a list an join with newlines ("\n"):

import pandas as pd

text = """The bullet pierced the window shattering it before missing Danny's head by mere millimeters.
Being unacquainted with the chief raccoon was harming his prospects for promotion.
There were white out conditions in the town; subsequently, the roads were impassable.
The hawk didn’t understand why the ground squirrels didn’t want to be his friend.
Nobody loves a pig wearing lipstick."""

df = pd.DataFrame({"id": list(range(5)), "text": text.splitlines()})

Original output:

print(df["text"])

Yields:

0    The bullet pierced the window shattering it be...
1    Being unacquainted with the chief raccoon was ...
2    There were white out conditions in the town; s...
3    The hawk didn’t understand why the ground squi...
4                 Nobody loves a pig wearing lipstick.

Desired output:

print("\n".join(df["text"].to_list()))

Yields:

The bullet pierced the window shattering it before missing Danny's head by mere millimeters.
Being unacquainted with the chief raccoon was harming his prospects for promotion.
There were white out conditions in the town; subsequently, the roads were impassable.
The hawk didn’t understand why the ground squirrels didn’t want to be his friend.
Nobody loves a pig wearing lipstick.

Upvotes: 2

Related Questions