找回密码
 快速注册
搜索
查看: 41|回复: 0

[python]Use the $\verb|pandas|$ library to output LaTeX

[复制链接]

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

hbghlyj 发表于 2022-7-6 04:22 |阅读模式


... many people move material between actual LaTeX and MathJax and vice versa, either boy hand, or through automated processes like tex4ht, panda, etc., and inconsistencies cause trouble with that.
pandas documentation

pandas is an open source, BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language.



pandas.DataFrame.to_latex Render object to a LaTeX tabular, longtable, or nested table.

Requires \usepackage{booktabs}. The output can be copy/pasted into a main LaTeX document or read from an external file with \input{table.tex}.
>>> df = pd.DataFrame(dict(name=['Raphael', 'Donatello'],
...                   mask=['red', 'purple'],
...                   weapon=['sai', 'bo staff']))
>>> print(df.to_latex(index=False))  
\begin{tabular}{lll}
 \toprule
       name &    mask &    weapon \\
 \midrule
    Raphael &     red &       sai \\
  Donatello &  purple &  bo staff \\
\bottomrule
\end{tabular}



Pandas to LaTeX

Example 1
In this example, we will simply create a DataFrame which consists of the student names and their score in a math subject. After that, we will use the to_latex() function to the output as a LaTeX document. Now, let’s see the code below. Here the first line shows that we have imported the pandas’ library and created the dataframe afterward. Finally, the latex() function is used to show it as a latex document.
import pandas as pd
Marks = pd.DataFrame({'Student Name': ['Math Score'],
                    'Mathew': ['52'],
                    'Ribs': ['98'],
                    'Shawn':['79'],
                    'Danial':['92']})
 
print(Marks.to_latex(index = True, multirow = True))
Below is the output of the above code, which is in the LaTeX format. Observe that each line is presented in a complete formatted way of LaTeX. Developing a LaTeX document is very easy; however, python makes it easier to develop a LaTeX document as you do not have to remember multiple lines of code but just one word, and that is to_latex().

Output:
1.py:8: FutureWarning: In future versions `DataFrame.to_latex` is expected to utilise the base implementation of `Styler.to_latex` for formatting and rendering. The arguments signature may therefore change. It is recommended instead to use `DataFrame.style.to_latex` which also contains additional functionality.
  print(Marks.to_latex(index = True, multirow = True))
\begin{tabular}{llllll}
\toprule
{} & Student Name & Mathew & Ribs & Shawn & Danial \\
\midrule
0 &   Math Score &     52 &   98 &    79 &     92 \\
\bottomrule
\end{tabular}



Example 2

Let us demonstrate another example; here, we will add another column in the previously defined DataFrame. First, we have just defined student names and math scores; now, we will add the gender of the student in the DataFrame as well. Below is the code to add another column in the DataFrame:
import pandas as pd
Marks = pd.DataFrame({'Student Name': ['Math Score', 'Gender'],
                    'Mathew': ['52', 'Male'],
                    'Ribs': ['98', 'Female'],
                    'Shawn':['79', 'Male'],
                    'Danial':['92','Female']})
 
print(Marks.to_latex(index = True, multirow = True))


Output:
1.py:8: FutureWarning: In future versions `DataFrame.to_latex` is expected to utilise the base implementation of `Styler.to_latex` for formatting and rendering. The arguments signature may therefore change. It is recommended instead to use `DataFrame.style.to_latex` which also contains additional functionality.
  print(Marks.to_latex(index = True, multirow = True))
\begin{tabular}{llllll}
\toprule
{} & Student Name & Mathew &    Ribs & Shawn &  Danial \\
\midrule
0 &   Math Score &     52 &      98 &    79 &      92 \\
1 &       Gender &   Male &  Female &  Male &  Female \\
\bottomrule
\end{tabular}


Example 3

In this example, we will make you learn another way of presenting the same information in a different way. In previous examples, we have demonstrated the data column-wise. Now, we will demonstrate the input information in a DataFrame in a row-wise format. The code is given below to show the data in a row format.
import pandas as pd
df = pd.DataFrame(dict(SName=['Mathew', 'Ribs', 'Shawn', 'Danial'],
                  Color=['red', 'White', 'purple', 'blue'],
                  marks=['50', '90', '80', '100']))
print(df.to_latex(index=False))


Output:
1.py:5: FutureWarning: In future versions `DataFrame.to_latex` is expected to utilise the base implementation of `Styler.to_latex` for formatting and rendering. The arguments signature may therefore change. It is recommended instead to use `DataFrame.style.to_latex` which also contains additional functionality.
  print(df.to_latex(index=False))
\begin{tabular}{lll}
\toprule
 SName &  Color & marks \\
\midrule
Mathew &    red &    50 \\
  Ribs &  White &    90 \\
 Shawn & purple &    80 \\
Danial &   blue &   100 \\
\bottomrule
\end{tabular}

手机版|悠闲数学娱乐论坛(第3版)

GMT+8, 2025-3-4 15:31

Powered by Discuz!

× 快速回复 返回顶部 返回列表