Programmatically convert pandas dataframe to markdown table

后端 未结 13 1174
梦毁少年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: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  |
    
    0 讨论(0)
  • 2020-12-13 04:22

    Improving the answer further, for use in IPython Notebook:

    def pandas_df_to_markdown_table(df):
        from IPython.display import Markdown, display
        fmt = ['---' for i in range(len(df.columns))]
        df_fmt = pd.DataFrame([fmt], columns=df.columns)
        df_formatted = pd.concat([df_fmt, df])
        display(Markdown(df_formatted.to_csv(sep="|", index=False)))
    
    pandas_df_to_markdown_table(infodf)
    

    Or use tabulate:

    pip install tabulate
    

    Examples of use are in the documentation.

    0 讨论(0)
  • 2020-12-13 04:23

    Try this out. I got it to work.

    See the screenshot of my markdown file converted to HTML at the end of this answer.

    import pandas as pd
    
    # You don't need these two lines
    # as you already have your DataFrame in memory
    df = pd.read_csv("nor.txt", sep="|")
    df.drop(df.columns[-1], axis=1)
    
    # Get column names
    cols = df.columns
    
    # Create a new DataFrame with just the markdown
    # strings
    df2 = pd.DataFrame([['---',]*len(cols)], columns=cols)
    
    #Create a new concatenated DataFrame
    df3 = pd.concat([df2, df])
    
    #Save as markdown
    df3.to_csv("nor.md", sep="|", index=False)
    

    0 讨论(0)
  • 2020-12-13 04:26

    Export a DataFrame to markdown

    I created the following function for exporting a pandas.DataFrame to markdown in Python:

    def df_to_markdown(df, float_format='%.2g'):
        """
        Export a pandas.DataFrame to markdown-formatted text.
        DataFrame should not contain any `|` characters.
        """
        from os import linesep
        return linesep.join([
            '|'.join(df.columns),
            '|'.join(4 * '-' for i in df.columns),
            df.to_csv(sep='|', index=False, header=False, float_format=float_format)
        ]).replace('|', ' | ')
    

    This function may not automatically fix the encoding issues of the OP, but that is a different issue than converting from pandas to markdown.

    0 讨论(0)
  • 2020-12-13 04:31

    I have tried several of the above solutions in this post and found this worked most consistently.

    To convert a pandas data frame to a markdown table I suggest using pytablewriter. Using the data provided in this post:

    import pandas as pd
    import pytablewriter
    from StringIO import StringIO
    
    c = StringIO("""ID, path,language, date,longest_sentence, shortest_sentence, number_words , readability_consensus 
    0, data/Eng/Sagitarius.txt , Eng, 2015-09-17 , With administrative experience in the prepa... , I am able to relocate internationally on short not..., 306, 11th and 12th grade
    31 , data/Nor/Høylandet.txt  , Nor, 2015-07-22 , Høgskolen i Østfold er et eksempel..., Som skuespiller har jeg både..., 253, 15th and 16th grade
    """)
    df = pd.read_csv(c,sep=',',index_col=['ID'])
    
    writer = pytablewriter.MarkdownTableWriter()
    writer.table_name = "example_table"
    writer.header_list = list(df.columns.values)
    writer.value_matrix = df.values.tolist()
    writer.write_table()
    

    This results in:

    # example_table
    ID |           path           |language|    date    |                longest_sentence                |                   shortest_sentence                  | number_words | readability_consensus 
    --:|--------------------------|--------|------------|------------------------------------------------|------------------------------------------------------|-------------:|-----------------------
      0| data/Eng/Sagitarius.txt  | Eng    | 2015-09-17 | With administrative experience in the prepa... | I am able to relocate internationally on short not...|           306| 11th and 12th grade   
     31| data/Nor/Høylandet.txt  | Nor    | 2015-07-22 | Høgskolen i Østfold er et eksempel...        | Som skuespiller har jeg både...                      |           253| 15th and 16th grade   
    

    Here is a markdown rendered screenshot.

    0 讨论(0)
  • 2020-12-13 04:32

    For those looking for how to do this using tabulate, I thought I'd put this here to save you some time:

    print(tabulate(df, tablefmt="pipe", headers="keys", showindex=False))

    0 讨论(0)
提交回复
热议问题