如何在终端中打印DataFrame而不丢失格式?

12

如何在终端上打印DataFrame而不失去其格式?

假设我有一个像这样的df:

In: df
Out:

    TFs No Esenciales  Genes regulados  Genes Regulados Positivamente  Genes Regulados Negativamente  No Tentativo de genes a silenciar  No Real de genes a silenciar  No Tentativo de genes a inducir
146              YdeO               20                             18                              2                              2                               2                               0
但是当我使用print在终端中显示时,它失去了格式。
In: print (df)
Out:
        TFs No Esenciales  Genes regulados  Genes Regulados Positivamente  \
146              YdeO               20                             18   

     Genes Regulados Negativamente  No Tentativo de genes a silenciar  \
146                              2                                 2   

     No Real de genes a silenciar  No Tentativo de genes a inducir  \
146                            2                               0   

     No Real de genes a inducir  Balance de genes  Balance real de genes  
146                          0                 2                      2  

如何在保留格式的情况下使用 print?

我期望的输出结果是:

In: print (df)
    Out:

    TFs No Esenciales  Genes regulados  Genes Regulados Positivamente  Genes Regulados Negativamente  No Tentativo de genes a silenciar  No Real de genes a silenciar  No Tentativo de genes a inducir
146              YdeO               20                             18                              2                              2                               2                               0

你尝试过 print(repr(df)) 吗? - Jean-François Fabre
2个回答

16

文档

有两个因素控制着您可能看到的格式。

  1. 控制显示器可以处理的字符宽度。

    • 这由pandas选项display.width处理,可以通过print pd.get_option('display.width')查看。默认值为80
  2. 第二个控制是要显示的数据帧中的列数。

    • 这由pandas选项display.max_columns处理,可以通过print pd.get_option('display.max_columns')查看。默认值为20

display.width

让我们通过一个示例数据框来探索它的作用。

import pandas as pd

df = pd.DataFrame([range(40)], columns=['ABCDE%d' % i for i in range(40)])

print df # this is with default 'display.width' of 80

   ABCDE0  ABCDE1  ABCDE2  ABCDE3  ABCDE4  ABCDE5  ABCDE6  ABCDE7  ABCDE8  \
0       0       1       2       3       4       5       6       7       8   

   ABCDE9   ...     ABCDE30  ABCDE31  ABCDE32  ABCDE33  ABCDE34  ABCDE35  \
0       9   ...          30       31       32       33       34       35   

   ABCDE36  ABCDE37  ABCDE38  ABCDE39  
0       36       37       38       39  

[1 rows x 40 columns]

pd.set_option('display.width', 40)

print df

   ABCDE0  ABCDE1  ABCDE2  ABCDE3  \
0       0       1       2       3   

   ABCDE4  ABCDE5  ABCDE6  ABCDE7  \
0       4       5       6       7   

   ABCDE8  ABCDE9   ...     ABCDE30  \
0       8       9   ...          30   

   ABCDE31  ABCDE32  ABCDE33  ABCDE34  \
0       31       32       33       34   

   ABCDE35  ABCDE36  ABCDE37  ABCDE38  \
0       35       36       37       38   

   ABCDE39  
0       39  

[1 rows x 40 columns]

pd.set_option('display.width', 120)

这将使内容向右滚动。

print df

   ABCDE0  ABCDE1  ABCDE2  ABCDE3  ABCDE4  ABCDE5  ABCDE6  ABCDE7  ABCDE8  ABCDE9   ...     ABCDE30  ABCDE31  ABCDE32  \
0       0       1       2       3       4       5       6       7       8       9   ...          30       31       32   

   ABCDE33  ABCDE34  ABCDE35  ABCDE36  ABCDE37  ABCDE38  ABCDE39  
0       33       34       35       36       37       38       39  

[1 rows x 40 columns]

display.max_columns

让我们使用 pd.set_option('display.width', 80)'display.width' 设置回 80。

现在让我们探索不同的 'display.max_columns' 值。

print df # default 20

   ABCDE0  ABCDE1  ABCDE2  ABCDE3  ABCDE4  ABCDE5  ABCDE6  ABCDE7  ABCDE8  \
0       0       1       2       3       4       5       6       7       8   

   ABCDE9   ...     ABCDE30  ABCDE31  ABCDE32  ABCDE33  ABCDE34  ABCDE35  \
0       9   ...          30       31       32       33       34       35   

   ABCDE36  ABCDE37  ABCDE38  ABCDE39  
0       36       37       38       39  

[1 rows x 40 columns]

注意中间的省略号。这个数据框有40列,为了显示最多20列,pandas取了前10列0:9和后10列30:39并在中间放置了省略号。

pd.set_option('display.max_columns', 30)

print df

   ABCDE0  ABCDE1  ABCDE2  ABCDE3  ABCDE4  ABCDE5  ABCDE6  ABCDE7  ABCDE8  \
0       0       1       2       3       4       5       6       7       8   

   ABCDE9  ABCDE10  ABCDE11  ABCDE12  ABCDE13  ABCDE14   ...     ABCDE25  \
0       9       10       11       12       13       14   ...          25   

   ABCDE26  ABCDE27  ABCDE28  ABCDE29  ABCDE30  ABCDE31  ABCDE32  ABCDE33  \
0       26       27       28       29       30       31       32       33   

   ABCDE34  ABCDE35  ABCDE36  ABCDE37  ABCDE38  ABCDE39  
0       34       35       36       37       38       39  

[1 rows x 40 columns]

注意字符宽度保持不变,但我有更多的列。pandas取了前15列0:14和最后15列26:39
要显示所有列,您需要将此选项设置为至少与您要显示的列数一样大。

pd.set_option('display.max_columns', 40)

print df

   ABCDE0  ABCDE1  ABCDE2  ABCDE3  ABCDE4  ABCDE5  ABCDE6  ABCDE7  ABCDE8  \
0       0       1       2       3       4       5       6       7       8   

   ABCDE9  ABCDE10  ABCDE11  ABCDE12  ABCDE13  ABCDE14  ABCDE15  ABCDE16  \
0       9       10       11       12       13       14       15       16   

   ABCDE17  ABCDE18  ABCDE19  ABCDE20  ABCDE21  ABCDE22  ABCDE23  ABCDE24  \
0       17       18       19       20       21       22       23       24   

   ABCDE25  ABCDE26  ABCDE27  ABCDE28  ABCDE29  ABCDE30  ABCDE31  ABCDE32  \
0       25       26       27       28       29       30       31       32   

   ABCDE33  ABCDE34  ABCDE35  ABCDE36  ABCDE37  ABCDE38  ABCDE39  
0       33       34       35       36       37       38       39  

所有列都不省略,全部显示。

将两个选项结合使用

此时非常简单。 pd.set_option('display.width', 1000) 使用1000以允许更长的内容。 pd.set_option('display.max_columns', 1000) 还可以用于显示宽数据框。

print df

   ABCDE0  ABCDE1  ABCDE2  ABCDE3  ABCDE4  ABCDE5  ABCDE6  ABCDE7  ABCDE8  ABCDE9  ABCDE10  ABCDE11  ABCDE12  ABCDE13  ABCDE14  ABCDE15  ABCDE16  ABCDE17  ABCDE18  ABCDE19  ABCDE20  ABCDE21  ABCDE22  ABCDE23  ABCDE24  ABCDE25  ABCDE26  ABCDE27  ABCDE28  ABCDE29  ABCDE30  ABCDE31  ABCDE32  ABCDE33  ABCDE34  ABCDE35  ABCDE36  ABCDE37  ABCDE38  ABCDE39
0       0       1       2       3       4       5       6       7       8       9       10       11       12       13       14       15       16       17       18       19       20       21       22       23       24       25       26       27       28       29       30       31       32       33       34       35       36       37       38       39

使用您的数据

print df

   TFs    No  Esenciales  Genes  regulados  Genes.1  Regulados  Positivamente  Genes.2  Regulados.1  Negativamente  No.1  Tentativo  de  genes   a  silenciar  No.2  Real  de.1  genes.1  a.1  silenciar.1  No.3  Tentativo.1  de.2  genes.2  a.2  inducir
0  146  YdeO          20     18          2        2          2              0      NaN          NaN            NaN   NaN        NaN NaN    NaN NaN        NaN   NaN   NaN   NaN      NaN  NaN          NaN   NaN          NaN   NaN      NaN  NaN      NaN

重要注意事项

运行此代码时,您可能无法看到此处的滚动效果。这是因为您的终端可能无法向右滚动。下面是来自jupyter-notebook的屏幕截图。它看起来不正确,因为文本被换行。然而,字符串中没有新行符号,因为当我将其复制并粘贴到堆栈溢出时,它会正确地显示。

enter image description here


有没有可能将 pd.set_option( ...) 写入某个文件中,这样我们就不需要每次都输入它们了? - nos
1
@nos https://ipython.org/ipython-doc/1/config/overview.html#startup-files请参考上述链接,了解有关启动文件的概述。 - piRSquared

4

有一些显示选项可用于控制如何打印DataFrame。您可能需要使用以下选项:

In [28]: pd.set_option('expand_frame_repr', False)

In [29]: pd.set_option('display.max_columns', 999)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接