7 python text visualization pandas
我有一个数据框,其中包含一列长文本。
为了演示它的外观(请注意文本应继续的省略号“...”):
id text group
123 My name is Benji and I ... 2
Run Code Online (Sandbox Code Playgroud)
上面的文字实际上比该短语更长。例如它可以是:
我叫本吉,住在堪萨斯州。
实际文本比这长得多。
当我尝试仅对文本列进行子集化时,它仅显示带有点“...”的部分文本。
我需要确保显示全文以供稍后进行文本摘要。但我不确定在选择文本列时如何显示全文。
我的df['text']
输出看起来像这样:
1 My name is Benji and I ...
2 He went to the creek and ...
Run Code Online (Sandbox Code Playgroud)
如何显示全文而不显示索引号?
dev*_*dit 14
您可以使用pd.set_option
withdisplay.max_colwidth
来显示自动换行符和多行单元格:
\n\ndisplay.max_colwidthint 或 None
\npandas 数据结构的 repr 中列的最大宽度(以字符为单位)。当列溢出时,\xe2\x80\x9c\xe2\x80\xa6\xe2\x80\x9d 占位符会嵌入到输出中。\xe2\x80\x98None\xe2\x80\x99 值表示无限制。[默认值:50]
\n
所以在你的情况下:
\npd.set_option(\'display.max_colwidth\', None)
对于旧版本,例如版本 0.22,请使用-1
而不是None
您可以将带有换行符 ( ) 的连接转换为列表"\\n"
:
import pandas as pd\n\ntext = """The bullet pierced the window shattering it before missing Danny's head by mere millimeters.\nBeing unacquainted with the chief raccoon was harming his prospects for promotion.\nThere were white out conditions in the town; subsequently, the roads were impassable.\nThe hawk didn\xe2\x80\x99t understand why the ground squirrels didn\xe2\x80\x99t want to be his friend.\nNobody loves a pig wearing lipstick."""\n\ndf = pd.DataFrame({"id": list(range(5)), "text": text.splitlines()})\n
Run Code Online (Sandbox Code Playgroud)\n原始输出:
\nprint(df["text"])\n
Run Code Online (Sandbox Code Playgroud)\n产量:
\n0 The bullet pierced the window shattering it be...\n1 Being unacquainted with the chief raccoon was ...\n2 There were white out conditions in the town; s...\n3 The hawk didn\xe2\x80\x99t understand why the ground squi...\n4 Nobody loves a pig wearing lipstick.\n
Run Code Online (Sandbox Code Playgroud)\n期望的输出:
\nprint("\\n".join(df["text"].to_list()))\n
Run Code Online (Sandbox Code Playgroud)\n产量:
\nThe bullet pierced the window shattering it before missing Danny's head by mere millimeters.\nBeing unacquainted with the chief raccoon was harming his prospects for promotion.\nThere were white out conditions in the town; subsequently, the roads were impassable.\nThe hawk didn\xe2\x80\x99t understand why the ground squirrels didn\xe2\x80\x99t want to be his friend.\nNobody loves a pig wearing lipstick.\n
Run Code Online (Sandbox Code Playgroud)\n
归档时间: |
|
查看次数: |
14970 次 |
最近记录: |