问题:在pandas数据框中完全打印很长的字符串

我正在努力看似非常简单的事情。我有一个包含非常长字符串的pandas数据框。

df = pd.DataFrame({'one' : ['one', 'two', 
      'This is very long string very long string very long string veryvery long string']})

现在,当我尝试打印相同的字符串时,我看不到完整的字符串,而只看到了字符串的一部分。

我尝试了以下选项

  • 使用 print(df.iloc[2])
  • 使用 to_html
  • 使用 to_string
  • 其中一个stackoverflow答案建议通过使用pandas display选项来增加列宽,但该方法也不起作用。
  • 我也没有得到如何set_printoptions帮助我。

任何想法表示赞赏。看起来很简单,但无法获得!

I am struggling with the seemingly very simple thing.I have a pandas data frame containing very long string.

df = pd.DataFrame({'one' : ['one', 'two', 
      'This is very long string very long string very long string veryvery long string']})

Now when I try to print the same, I do not see the full string I rather see only part of the string.

I tried following options

  • using print(df.iloc[2])
  • using to_html
  • using to_string
  • One of the stackoverflow answer suggested to increase column width by using pandas display option, that did not work either.
  • I also did not get how set_printoptions will help me.

Any ideas appreciated. Looks very simple, but not able to get it!


回答 0

您可以使用options.display.max_colwidth指定想要在默认表示中看到更多内容:

In [2]: df
Out[2]:
                                                 one
0                                                one
1                                                two
2  This is very long string very long string very...

In [3]: pd.options.display.max_colwidth
Out[3]: 50

In [4]: pd.options.display.max_colwidth = 100

In [5]: df
Out[5]:
                                                                               one
0                                                                              one
1                                                                              two
2  This is very long string very long string very long string veryvery long string

实际上,如果您只想检查一个值,则可以通过访问它(作为标量,而不是像一行一样df.iloc[2])来查看完整的字符串:

In [7]: df.iloc[2,0]    # or df.loc[2,'one']
Out[7]: 'This is very long string very long string very long string veryvery long string'

You can use options.display.max_colwidth to specify you want to see more in the default representation:

In [2]: df
Out[2]:
                                                 one
0                                                one
1                                                two
2  This is very long string very long string very...

In [3]: pd.options.display.max_colwidth
Out[3]: 50

In [4]: pd.options.display.max_colwidth = 100

In [5]: df
Out[5]:
                                                                               one
0                                                                              one
1                                                                              two
2  This is very long string very long string very long string veryvery long string

And indeed, if you just want to inspect the one value, by accessing it (as a scalar, not as a row as df.iloc[2] does) you also see the full string:

In [7]: df.iloc[2,0]    # or df.loc[2,'one']
Out[7]: 'This is very long string very long string very long string veryvery long string'

回答 1

使用pd.set_option('display.max_colwidth', -1)自动换行,多行细胞。

是有关如何充分利用大熊猫的jupyters显示器的重要资源。

Use pd.set_option('display.max_colwidth', -1) for automatic linebreaks and multi-line cells.

This is a great resource on how to use jupyters display with pandas to the fullest.


回答 2

另一种非常简单的方法是调用列表函数:

list(df['one'][2])
# output:
['This is very long string very long string very long string veryvery long string']

值得一提的是,要列出整个列并不是很方便,但是对于简单的一行来说,为什么呢?

Another, pretty simple approach is to call list function:

list(df['one'][2])
# output:
['This is very long string very long string very long string veryvery long string']

No worth to mention, that is not good to convent to list the whole columns, but for a simple line – why not


回答 3

打印整个字符串的另一种简便方法是values在数据框上调用。

df = pd.DataFrame({'one' : ['one', 'two', 
      'This is very long string very long string very long string veryvery long string']})

print(df.values)

输出将是

[['one']
 ['two']
 ['This is very long string very long string very long string veryvery long string']]

Another easier way to print the whole string is to call values on the dataframe.

df = pd.DataFrame({'one' : ['one', 'two', 
      'This is very long string very long string very long string veryvery long string']})

print(df.values)

The Output will be

[['one']
 ['two']
 ['This is very long string very long string very long string veryvery long string']]

回答 4

这是你的本意吗?

In [7]: x =  pd.DataFrame({'one' : ['one', 'two', 'This is very long string very long string very long string veryvery long string']})

In [8]: x
Out[8]: 
                                                 one
0                                                one
1                                                two
2  This is very long string very long string very...

In [9]: x['one'][2]
Out[9]: 'This is very long string very long string very long string veryvery long string'

Is this what you meant to do ?

In [7]: x =  pd.DataFrame({'one' : ['one', 'two', 'This is very long string very long string very long string veryvery long string']})

In [8]: x
Out[8]: 
                                                 one
0                                                one
1                                                two
2  This is very long string very long string very...

In [9]: x['one'][2]
Out[9]: 'This is very long string very long string very long string veryvery long string'

回答 5

我经常处理您描述的情况的.to_csv()方法是使用该方法并写入stdout:

import sys

df.to_csv(sys.stdout)

更新:现在应该可以使用None而不是sys.stdout具有相似的效果了!

这应该转储整个数据帧,包括所有字符串的全部。您可以使用to_csv参数来配置列分隔符,是否打印索引等。不过,它不如正确呈现它漂亮。

我最初将其发布是为了回答有关熊猫中某个数据框中所有列的输出数据的一些相关问题

The way I often deal with the situation you describe is to use the .to_csv() method and write to stdout:

import sys

df.to_csv(sys.stdout)

Update: it should now be possible to just use None instead of sys.stdout with similar effect!

This should dump the whole dataframe, including the entirety of any strings. You can use the to_csv parameters to configure column separators, whether the index is printed, etc. It will be less pretty than rendering it properly though.

I posted this originally in answer to the somewhat-related question at Output data from all columns in a dataframe in pandas


回答 6

只需在打印之前将以下行添加到您的代码中即可。

 pd.options.display.max_colwidth = 90  # set a value as your need

您只需执行以下步骤即可设置其他附加选项,

  • 您可以如下更改熊猫max_columns功能的选项,以显示更多列

    import pandas as pd
    pd.options.display.max_columns = 10

    (这将显示10列,您可以根据需要进行更改)

  • 这样,您可以更改行数,如下所示以显示更多行

    pd.options.display.max_rows = 999

    (这允许一次打印999行)

这应该很好

请参考文档,为熊猫更改更多选项/设置

Just add the following line to your code before print.

 pd.options.display.max_colwidth = 90  # set a value as your need

You can simply do the following steps for setting other additional options,

  • You can change the options for pandas max_columns feature as follows to display more columns

    import pandas as pd
    pd.options.display.max_columns = 10
    

    (this allows 10 columns to display, you can change this as you need)

  • Like that you can change the number of rows as you need to display as follows to display more rows

    pd.options.display.max_rows = 999
    

    (this allows to print 999 rows at a time)

this should works fine

Please kindly refer the doc to change more options/settings for pandas


回答 7

我创建了一个小实用程序功能,对我来说效果很好

def display_text_max_col_width(df, width):
    with pd.option_context('display.max_colwidth', width):
        print(df)

display_text_max_col_width(train_df["Description"], 800)

我可以根据需要更改宽度的长度,而无需永久设置任何选项。

I have created a small utility function, this works well for me

def display_text_max_col_width(df, width):
    with pd.option_context('display.max_colwidth', width):
        print(df)

display_text_max_col_width(train_df["Description"], 800)

I can change length of the width as per my requirement, without setting any option permanently.


回答 8

如果您使用的是jupyter笔记本,还可以将pandas数据帧打印为HTML表格,该表格将打印完整字符串。

from IPython.display import display, HTML
display(HTML(df.to_html()))

输出量

    one
0   one
1   two
2   This is very long string very long string very long string veryvery long string

If you’re using jupyter notebook, you can also print pandas dataframe as HTML table, which will print full strings.

from IPython.display import display, HTML
display(HTML(df.to_html()))

Output

    one
0   one
1   two
2   This is very long string very long string very long string veryvery long string

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。