如何在 Pandas 中显示列的全文
How to display the full-text of a column in Pandas
我有一个包含长文本列的数据框。
演示它的外观(注意文本应继续的省略号“...”):
id text group
123 My name is Benji and I ... 2
上面的文字实际上比那个词组要长。例如它可以是:
My name is Benji and I am living in Kansas.
实际的文字比这长得多。
当我尝试仅对文本列进行子集化时,它只显示带有点“...”的部分文本。
我需要确保显示全文以便稍后进行文本摘要。
但是我不确定在选择文本栏时如何显示全文。
我的 df['text']
输出看起来像这样:
1 My name is Benji and I ...
2 He went to the creek and ...
如何显示全文而不显示索引号?
您可以使用 pd.set_option
和 display.max_colwidth
来显示自动换行和多行单元格:
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]
所以在你的情况下:
pd.set_option('display.max_colwidth', None)
对于 older versions, like version 0.22,使用 -1
而不是 None
您可以将换行符连接转换为列表 ("\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.
我有一个包含长文本列的数据框。
演示它的外观(注意文本应继续的省略号“...”):
id text group
123 My name is Benji and I ... 2
上面的文字实际上比那个词组要长。例如它可以是:
My name is Benji and I am living in Kansas.
实际的文字比这长得多。
当我尝试仅对文本列进行子集化时,它只显示带有点“...”的部分文本。
我需要确保显示全文以便稍后进行文本摘要。 但是我不确定在选择文本栏时如何显示全文。
我的 df['text']
输出看起来像这样:
1 My name is Benji and I ...
2 He went to the creek and ...
如何显示全文而不显示索引号?
您可以使用 pd.set_option
和 display.max_colwidth
来显示自动换行和多行单元格:
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]
所以在你的情况下:
pd.set_option('display.max_colwidth', None)
对于 older versions, like version 0.22,使用 -1
而不是 None
您可以将换行符连接转换为列表 ("\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.