Pretty-print an entire Pandas Series / DataFrame

后端 未结 12 2192
再見小時候
再見小時候 2020-11-22 03:26

I work with Series and DataFrames on the terminal a lot. The default __repr__ for a Series returns a reduced sample, with some head and tail values, but the res

12条回答
  •  心在旅途
    2020-11-22 04:14

    Use the tabulate package:

    pip install tabulate
    

    And consider the following example usage:

    import pandas as pd
    from io import StringIO
    from tabulate import tabulate
    
    c = """Chromosome Start End
    chr1 3 6
    chr1 5 7
    chr1 8 9"""
    
    df = pd.read_table(StringIO(c), sep="\s+", header=0)
    
    print(tabulate(df, headers='keys', tablefmt='psql'))
    
    +----+--------------+---------+-------+
    |    | Chromosome   |   Start |   End |
    |----+--------------+---------+-------|
    |  0 | chr1         |       3 |     6 |
    |  1 | chr1         |       5 |     7 |
    |  2 | chr1         |       8 |     9 |
    +----+--------------+---------+-------+
    

提交回复
热议问题