如何在Pandas中显示列的全文

h9a6wy2h  于 2023-05-12  发布在  其他
关注(0)|答案(3)|浏览(112)

我有一个数据框,其中包含一个包含长文本的列。
要演示它的外观(请注意省略号“...”,文本应继续):

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

上面的文字实际上比这句话长。例如,它可以是:
我的名字是班吉,我住在堪萨斯州。
实际的文本比这长得多。
当我尝试只对文本列进行子集化时,它只显示带有点“...”的部分文本。
我需要确保全文显示文本sumarization后。但我不知道如何显示全文时,选择文本列。
我的df['text']输出看起来像这样:
我如何显示全文和没有索引号?

vshtjzan

vshtjzan1#

您可以将pd.set_optiondisplay.max_colwidth配合使用,以显示自动换行和多行单元格:
display.max_colwidthint或None
pandas数据结构中repr中列的最大字符宽度。当列溢出时,输出中会嵌入一个“...”占位符。“无”值表示无限制。[默认值:50]
在你的例子中:
pd.set_option('display.max_colwidth', None)
对于较旧的版本,如版本0.22,使用-1而不是None

gdrx4gfi

gdrx4gfi2#

您可以使用换行符("\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()})

原始输出:

print(df["text"])

产量:

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.

所需输出:

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

产量:

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

gopyfrb33#

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

相关问题