Python 在熊猫数据框中完全打印很长的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29902714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:09:16  来源:igfitidea点击:

Print very long string completely in pandas dataframe

pythonstringpandasoptions

提问by Yantraguru

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_printoptionswill help me.
  • 使用 print(df.iloc[2])
  • 使用 to_html
  • 使用 to_string
  • stackoverflow 的一个答案建议通过使用 pandas 显示选项来增加列宽,这也不起作用。
  • 我也没有得到如何set_printoptions帮助我。

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

任何想法表示赞赏。看起来很简单,但是搞不定!

采纳答案by joris

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

您可以使用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

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:

事实上,如果您只想检查一个值,通过访问它(作为标量,而不是作为一行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'

回答by fixxxer

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'

回答by user2428107

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

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

import sys

df.to_csv(sys.stdout)

Update: it should now be possible to just use Noneinstead of sys.stdoutwith similar effect!

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

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.

这应该转储整个数据帧,包括所有字符串的全部。您可以使用 to_csv 参数来配置列分隔符、是否打印索引等。不过它会比正确渲染它更不美观。

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

我最初发布此内容是为了回答在 Pandas 中的数据框中所有列的输出数据中有些相关的问题

回答by Rocketq

Another, pretty simple approach is to call list function:

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

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

不值得一提,修道院列出整列是不好的,但对于简单的一行 - 为什么不

回答by bigbounty

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

打印整个字符串的另一种更简单的方法是调用values数据帧。

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']]

回答by omnesia

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

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

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

是关于如何最充分地使用 jupyter 显示与 Pandas 的重要资源。

回答by Amila Viraj

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)

  • 您可以按如下方式更改 pandas max_columns 功能的选项以显示更多列

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

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

  • 像这样,您可以根据需要更改行数,如下所示以显示更多行

    pd.options.display.max_rows = 999
    

    (这允许一次打印 999 行)

this should works fine

这应该可以正常工作

Please kindly refer the docto change more options/settings for pandas

请参阅文档以更改熊猫的更多选项/设置

回答by Sachin Rastogi

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.

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