Programmatically convert pandas dataframe to markdown table

后端 未结 13 1176
梦毁少年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条回答
  • 2020-12-13 04:41

    I recommend python-tabulate library for generating ascii-tables. The library supports pandas.DataFrame as well.

    Here is how to use it:

    from pandas import DataFrame
    from tabulate import tabulate
    
    df = DataFrame({
        "weekday": ["monday", "thursday", "wednesday"],
        "temperature": [20, 30, 25],
        "precipitation": [100, 200, 150],
    }).set_index("weekday")
    
    print(tabulate(df, tablefmt="pipe", headers="keys"))
    

    Output:

    | weekday   |   temperature |   precipitation |
    |:----------|--------------:|----------------:|
    | monday    |            20 |             100 |
    | thursday  |            30 |             200 |
    | wednesday |            25 |             150 |
    
    0 讨论(0)
提交回复
热议问题