Export a LaTeX table from pandas DataFrame

前端 未结 3 1325
北荒
北荒 2020-12-12 15:10

Is there an easy way to export a data frame (or even a part of it) to LaTeX?

I searched in google and was only able to find solutions using asciitables.

3条回答
  •  半阙折子戏
    2020-12-12 15:30

    Just write to a textfile. It's no magic:

    import pandas as pd
    df = pd.DataFrame({"a":range(10), "b":range(10,20)})
    with open("my_table.tex", "w") as f:
        f.write("\\begin{tabular}{" + " | ".join(["c"] * len(df.columns)) + "}\n")
        for i, row in df.iterrows():
            f.write(" & ".join([str(x) for x in row.values]) + " \\\\\n")
        f.write("\\end{tabular}")
    

提交回复
热议问题