Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export pandas DataFrame to LaTeX and apply formatters by row

I want to export some DataFrames to LaTeX but these DataFrames have lots of columns, but not that many items. The solution is to display the table transposed. I know about pandas' transpose, but I want to format my rows and the method to_latex supports only formatters by column.

Some code:

import pandas as pd

names = ['Bob','Jessica','Mary','John','Mel']
births = [968, 155, 77, 578, 973]
tralala = [0.1, 0.2, 0.3, 0.4, 0.5]
BabyDataSet = zip(names,births,tralala)
df = pd.DataFrame(data = BabyDataSet, columns=['Names', 'Births', 'Whatever'])
df = df.transpose()
print df.to_latex()

Output:

\begin{tabular}{llllll}
\toprule
{} &    0 &        1 &     2 &     3 &    4 \\
\midrule
Names    &  Bob &  Jessica &  Mary &  John &  Mel \\
Births   &  968 &      155 &    77 &   578 &  973 \\
Whatever &  0.1 &      0.2 &   0.3 &   0.4 &  0.5 \\
\bottomrule
\end{tabular}

But what if I want to this for example:

\begin{tabular}{llllll}
\toprule
{} &    0 &        1 &     2 &     3 &    4 \\
\midrule
Names    &  Bob   &  Jessica &  Mary &  John &  Mel \\
Births   &  9.7e2 &    1.6e2 & 7.7e1 & 5.8e2 &  9.7e2 \\
Whatever &  0.1   &      0.2 &   0.3 &   0.4 &  0.5 \\
\bottomrule
\end{tabular}

Is there any way to hack this functionality? Only thing I thought about was to manually apply the formats converting all my columns to strings before transposing and exporting

like image 496
gsmafra Avatar asked Aug 15 '15 00:08

gsmafra


People also ask

How to export pandas Dataframe to excel?

You can export Pandas DataFrame to an Excel file using to_excel. Here is a template that you may apply in Python to export your DataFrame: df.to_excel(r'Path where the exported excel file will be storedFile Name.xlsx', index = False) And if you want to export your DataFrame to a specific Excel Sheet, then you may use this template:

What is pandas Dataframe-to_latex () function?

Pandas DataFrame - to_latex() function: The to_latex() function is used to render an object to a LaTeX tabular environment table.

What is the default column format in latex tables?

The columns format as specified in LaTeX table format e.g. 'rcl' for 3 columns. By default, 'l' will be used for all columns except columns of numbers, which default to 'r'. By default, the value will be read from the pandas config module. Use a longtable environment instead of tabular.

Is it possible to export tabular data to LaTeX using plain Python?

7 Although it is useful to show how easy it is to export tabular data to LaTeX using plain Python, it is of course much better to use existing facilities, i.e., DataFrame.to_latex()in this case. – hans_meine Mar 12 '14 at 10:49


1 Answers

I came up with this:

for key in df.columns.values:
    if key in formatters:
        df[key] = df[key].apply(formatters[key])
print df.to_latex()

This is pretty much equivalent to

print df.to_latex(formatters=formatters)

and works with transposed DataFrames as well i.e. df.T.to_latex()

like image 145
gsmafra Avatar answered Oct 27 '22 00:10

gsmafra