如何在 Pandas 中显示列的全文

Posted

技术标签:

【中文标题】如何在 Pandas 中显示列的全文【英文标题】:How to display the full-text of a column in Pandas 【发布时间】:2021-09-30 02:00:07 【问题描述】:

我有一个包含长文本列的数据框。

为了演示它的外观(注意省略号“...”,文本应该继续):

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

上面的文字实际上比那个短语长。例如它可能是:

我叫 Benji,住在堪萨斯州。

实际的文字比这长得多。

当我尝试仅对文本列进行子集化时,它只显示带有点“...”的部分文本。

我需要确保显示全文以便稍后进行文本摘要。 但是我不确定在选择文本列时如何显示全文。

我的df['text'] 输出如下所示:

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

如何在没有索引号的情况下显示全文?

【问题讨论】:

你用什么代码来产生这个输出? 只是一个简单的 df['text'] 以便我可以将其分配为变量“句子 ***.com/questions/29902714/… 有没有办法不打印索引号? ***.com/questions/24644656/… 【参考方案1】:

您可以使用pd.set_optiondisplay.max_colwidth 来显示自动换行符和多行单元格:

display.max_colwidthint 或无

pandas 数据结构的 repr 中列的最大字符宽度。当列溢出时,会在输出中嵌入一个“...”占位符。 “无”值意味着无限。 [默认:50]

所以在你的情况下:

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

对于older versions, like version 0.22,使用-1 而不是None

【讨论】:

这个答案解决了显示的问题,同时不修改数据本身?️ 您可以使用上下文仅在需要时应用此显示选项,而不是无处不在;类似:with pd.option_context("display.max_colwidth", None): ...【参考方案2】:

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

【讨论】:

实际上你甚至不需要to_list()print("\n".join(df["text"])) 工作原理相同。

以上是关于如何在 Pandas 中显示列的全文的主要内容,如果未能解决你的问题,请参考以下文章

如何在 pandas / matplotlib 中绘制多条线

如何在 Pandas 中获得两列的组合? [复制]

在 Pandas 中,如何根据其他列的共同相互关系创建唯一 ID?

如何在整个 Pandas 数据框中搜索字符串并获取包含它的列的名称?

在 pandas 中,如何在具有匹配行和列的 3 个单独数据帧之间建立相关矩阵?

我们如何在列的每个值中对 pandas 数据框进行二次采样