Programmatically convert pandas dataframe to markdown table

后端 未结 13 1302
梦毁少年i
梦毁少年i 2020-12-13 03:44

I have a Pandas Dataframe generated from a database, which has data with mixed encodings. For example:

+----+-------------------------+----------+-----------         


        
13条回答
  •  旧时难觅i
    2020-12-13 04:20

    Yet another solution. This time via thin wrapper around tabulate: tabulatehelper

    import numpy as np
    import pandas as pd
    import tabulatehelper as th
    
    df = pd.DataFrame(np.random.random(16).reshape(4, 4), columns=('a', 'b', 'c', 'd'))
    print(th.md_table(df, formats={-1: 'c'}))
    

    Output:

    |        a |        b |        c |        d |
    |---------:|---------:|---------:|:--------:|
    | 0.413284 | 0.932373 | 0.277797 | 0.646333 |
    | 0.552731 | 0.381826 | 0.141727 | 0.2483   |
    | 0.779889 | 0.012458 | 0.308352 | 0.650859 |
    | 0.301109 | 0.982111 | 0.994024 | 0.43551  |
    

提交回复
热议问题