【问题标题】:python how to nicely align long text in pandas dataframe?python如何很好地对齐熊猫数据框中的长文本?
【发布时间】:2017-10-31 16:43:32
【问题描述】:

给定熊猫的数据框(从数据库接收),我试图将结果输出到控制台,以使其完整可读

我面临的挑战是关于 2 列中的长文本:LPQ_REASON 和 LPQ_RESOLUTION。您会从下面的输出 (print df) 中注意到,两个 LPQ 列都以 3 个点 (...) 结尾,因此我无法阅读文本。尽管我的初始设置如下:

pd.set_option('display.max_rows', 1500)
pd.set_option('display.max_columns', 1500)
pd.set_option('display.width', 1000)

所以控制台上的结果如下所示:

       ID     DIS_CASE_ID                        CREATION_DATE  type_2                                  LPQ_REASON                                     LPQ_RESOLUTION   RESOLUTION_CODE
       0           727990         61180481 2017-01-05 13:47:05    7891  The LPQ we know is shorto add is 25% (h...  This Memo was issued with conjunction to our j...              3979
       1           727889         61180482 2017-01-05 13:51:09    7891  The LPQ he collide will increase 15% (h...  This Memo was issued on matching viloation for...              3979

我正在寻找的最佳解决方案(如果可行的话)是打印整行,这样:

           ID      DIS_CASE_ID                  CREATION_DATE  type_2                                  LPQ_REASON                                     LPQ_RESOLUTION     RESOLUTION_CODE
            0           727990   61180481 2017-01-05 13:47:05    7891   The LPQ we know is shorto add is 25% (here       This Memo was issued with conjunction to our               3979
                                                                        comes the rest of the sentence. it might be      analysis to foster a better bs when writing
                                                                        long, or not, it might be short or whatever)  

            1           727889   61180482 2017-01-05 13:51:09    7891  The LPQ he collide will increase 15% yes and  This Memo was issued on matching viloation for                 3979
                                                                       here I'm going to write the entire sentence    who cares on what violation. just issued. 
                                                                       as if I really remember what was written. ha

【问题讨论】:

    标签: python pandas alignment indentation


    【解决方案1】:

    不如你所愿,但你可以尝试以下方法:

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

    其中 100 是您可以选择的列宽。但这不会创建多行单元格,而是创建一个很长的列。

    或:

    不是很优雅,但是 您可以尝试使用“制表”库 (https://pypi.python.org/pypi/tabulate) 来创建漂亮的文本表格,例如:

    +--------+-------+
    | item | qty |
    +========+=======+
    | spam | 42 |
    +--------+-------+
    | eggs | 451 |
    +--------+-------+
    | bacon | 0 |
    +--------+-------+
    

    使用制表符可以使用 '\n' 换行符。 只需遍历您的文本单元格并每 X 个字符放置一个“\n”(假设每 50 个字符)。

    一个简单的代码:

    for i in range(len(data)): 
        data.at[i,'text'] = data.at[i,'text'][0:50] + '\n' + data.at[i,'text'][50:]
    

    以上限制为只有一个换行符,但您可以改进它以对长文本进行多次换行。也不考虑是否在单词中间打断。

    !确保在数据副本上执行此操作,因为它会更改您的数据。如果您尝试使用常规的“打印”打印它,那么您会看到“\n”卡在文本中间!

    【讨论】:

      猜你喜欢
      • 2020-03-12
      • 2021-08-16
      • 2023-03-11
      • 1970-01-01
      • 2013-06-18
      • 2018-09-07
      • 2016-01-28
      • 2018-11-27
      • 1970-01-01
      相关资源
      最近更新 更多