如何在不剪切长参数的情况下打印pandas.dataframe.head

时间:2018-01-05 22:01:54

标签: pandas

例如,如何避免这种情况:

    column1                              column2 
0  [4623,236236,2362,626226, ...         64662626
1  [6363,554644,6346,346363, ...         73473473
2  [9543,543674,9806,134736, ...         67457733

1 个答案:

答案 0 :(得分:0)

display.max_colwidth选项设置为-1,查看set_options doc以获取详细信息。

pd.set_option('display.max_colwidth', -1)

示例代码

import random
import pandas as pd

d = { 'column1' : [random.sample(xrange(100), 100) for i in range(3)],
          ' column2' : [ random.randint(1000000,10000000) for i in range(3)]}


df = pd.DataFrame(d)

没有set_option

>>> df
    column2                                            column1
0   7746029  [63, 11, 86, 52, 48, 37, 47, 2, 25, 44, 72, 45...
1   9747244  [27, 34, 0, 88, 46, 39, 89, 78, 23, 1, 47, 9, ...
2   2387014  [45, 63, 26, 22, 7, 90, 92, 44, 99, 19, 17, 91...

使用set_option

>>> pd.set_option('display.max_colwidth', -1)
>>> df
    column2  \
0  7746029    
1  9747244    
2  

2387014 

column1  
    0  [63, 11, 86, 52, 48, 37, 47, 2, 25, 44, 72, 45, 36, 14, 31, 55, 21, 0, 93, 74, 30, 91, 6, 20, 54, 41, 40, 38, 79, 46, 73, 42, 97, 64, 81, 10, 77, 69, 3, 78, 70, 85, 84, 66, 4, 9, 50, 59, 98, 58, 22, 57, 18, 90, 95, 35, 12, 34, 99, 80, 1, 7, 87, 82, 33, 39, 68, 83, 27, 28, 76, 23, 17, 8, 92, 32, 5, 19, 62, 15, 65, 49, 53, 61, 43, 94, 13, 29, 89, 26, 24, 88, 71, 16, 96, 51, 67, 75, 60, 56]  
    1  [27, 34, 0, 88, 46, 39, 89, 78, 23, 1, 47, 9, 14, 70, 95, 44, 17, 30, 40, 82, 92, 58, 6, 3, 72, 52, 74, 84, 54, 67, 69, 97, 75, 90, 4, 24, 93, 94, 15, 43, 38, 73, 81, 22, 99, 64, 56, 33, 50, 83, 20, 80, 79, 57, 26, 32, 60, 2, 8, 49, 87, 10, 62, 85, 65, 11, 7, 16, 77, 45, 31, 91, 71, 28, 48, 13, 42, 86, 51, 41, 19, 59, 21, 18, 76, 66, 55, 12, 98, 36, 68, 53, 37, 96, 35, 5, 25, 29, 61, 63]  


 2  [45, 63, 26, 22, 7, 90, 92, 44, 99, 19, 17, 91, 57, 47, 88, 39, 9, 23, 59, 33, 94, 93, 66, 43, 11, 24, 18, 58, 97, 53, 15, 98, 29, 34, 48, 80, 40, 10, 87, 41, 49, 52, 86, 95, 0, 65, 3, 81, 84, 74, 61, 21, 20, 67, 8, 51, 55, 38, 69, 60, 27, 62, 72, 6, 4, 50, 46, 56, 82, 35, 2, 77, 68, 54, 89, 70, 75, 85, 16, 79, 32, 78, 31, 1, 42, 73, 12, 28, 30, 76, 37, 5, 83, 13, 64, 96, 14, 71, 25, 36]  
    >>>

EDIT-1

为了打印具有大量字符串/列表值的列,您可能需要在代码中添加以下设置。

pd.set_option('display.max_seq_items', None)

我尝试使用列值为10,000的列,并且打印效果很好。

根据documentation

  

display.max_seq_items:int或None

     

当漂亮地打印长序列时,不会再打印max_seq_items。如果省略了项目,则会在结果字符串中添加“...”来表示。

     

如果设置为“无”,则要打印的项目数不受限制。 [默认:100] [目前:100]