Programmatically convert pandas dataframe to markdown table

后端 未结 13 1266
梦毁少年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:36

    Pandas 1.0 was released 29 january 2020 and supports markdown conversion, so you can now do this directly!

    Example taken from the docs:

    df = pd.DataFrame({"A": [1, 2, 3], "B": [1, 2, 3]}, index=['a', 'a', 'b'])
    print(df.to_markdown())
    
    |    |   A |   B |
    |:---|----:|----:|
    | a  |   1 |   1 |
    | a  |   2 |   2 |
    | b  |   3 |   3 |
    

    Or without the index:

    print(df.to_markdown(index=False)) # use 'showindex' for pandas < 1.1
    
    |   A |   B |
    |----:|----:|
    |   1 |   1 |
    |   2 |   2 |
    |   3 |   3 |
    

提交回复
热议问题