How do I output lists as a table in Jupyter notebook?

后端 未结 11 1028
灰色年华
灰色年华 2021-01-30 09:51

I know that I\'ve seen some example somewhere before but for the life of me I cannot find it when googling around.

I have some rows of data:

data = [[1,2         


        
11条回答
  •  说谎
    说谎 (楼主)
    2021-01-30 10:30

    I recently used prettytable for rendering a nice ASCII table. It's similar to the postgres CLI output.

    import pandas as pd
    from prettytable import PrettyTable
    
    data = [[1,2,3],[4,5,6],[7,8,9]]
    df = pd.DataFrame(data, columns=['one', 'two', 'three'])
    
    def generate_ascii_table(df):
        x = PrettyTable()
        x.field_names = df.columns.tolist()
        for row in df.values:
            x.add_row(row)
        print(x)
        return x
    
    generate_ascii_table(df)
    

    Output:

    +-----+-----+-------+
    | one | two | three |
    +-----+-----+-------+
    |  1  |  2  |   3   |
    |  4  |  5  |   6   |
    |  7  |  8  |   9   |
    +-----+-----+-------+
    

提交回复
热议问题