4

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?

hc_dev
  • 8,389
  • 1
  • 26
  • 38

3 Answers3

10

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

display.max_colwidthint 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

hc_dev
  • 8,389
  • 1
  • 26
  • 38
devReddit
  • 2,696
  • 1
  • 5
  • 20
  • This answer solves the __displaying__ issue while not modifying the data itself ️ – hc_dev Jul 22 '21 at 20:32
  • 3
    You can use a context to apply this display option only when you want and not everywhere; something like: `with pd.option_context("display.max_colwidth", None): ...` – Rafs Dec 17 '21 at 10:48
1

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.
cosmic_inquiry
  • 2,557
  • 11
  • 23
0
dataframe.head(1)['columnname'].values
ABabin
  • 2,724
  • 24
  • 30
  • Answer needs supporting information Your answer could be improved with additional supporting information. Please [edit](https://stackoverflow.com/posts/76218765/edit) to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](https://stackoverflow.com/help/how-to-answer). – moken May 12 '23 at 11:10