Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.Series.to_latex#

Series.to_latex(buf=None,*,columns=None,header=True,index=True,na_rep='NaN',formatters=None,float_format=None,sparsify=None,index_names=True,bold_rows=False,column_format=None,longtable=None,escape=None,encoding=None,decimal='.',multicolumn=None,multicolumn_format=None,multirow=None,caption=None,label=None,position=None)[source]#

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

Requires\usepackage{{booktabs}}. The output can be copy/pastedinto a main LaTeX document or read from an external filewith\input{{table.tex}}.

Changed in version 2.0.0:Refactored to use the Styler implementation via jinja2 templating.

Parameters:
bufstr, Path or StringIO-like, optional, default None

Buffer to write to. If None, the output is returned as a string.

columnslist of label, optional

The subset of columns to write. Writes all columns by default.

headerbool or list of str, default True

Write out the column names. If a list of strings is given,it is assumed to be aliases for the column names.

indexbool, default True

Write row names (index).

na_repstr, default ‘NaN’

Missing data representation.

formatterslist of functions or dict of {{str: function}}, optional

Formatter functions to apply to columns’ elements by position orname. The result of each function must be a unicode string.List must be of length equal to the number of columns.

float_formatone-parameter function or str, optional, default None

Formatter for floating point numbers. For examplefloat_format="%.2f" andfloat_format="{{:0.2f}}".format willboth result in 0.1234 being formatted as 0.12.

sparsifybool, optional

Set to False for a DataFrame with a hierarchical index to printevery multiindex key at each row. By default, the value will beread from the config module.

index_namesbool, default True

Prints the names of the indexes.

bold_rowsbool, default False

Make the row labels bold in the output.

column_formatstr, optional

The columns format as specified inLaTeX table format e.g. ‘rcl’ for 3columns. By default, ‘l’ will be used for all columns exceptcolumns of numbers, which default to ‘r’.

longtablebool, optional

Use a longtable environment instead of tabular. Requiresadding a usepackage{{longtable}} to your LaTeX preamble.By default, the value will be read from the pandas configmodule, and set toTrue if the optionstyler.latex.environment is“longtable”.

Changed in version 2.0.0:The pandas option affecting this argument has changed.

escapebool, optional

By default, the value will be read from the pandas configmodule and set toTrue if the optionstyler.format.escape is“latex”. When set to False prevents from escaping latex specialcharacters in column names.

Changed in version 2.0.0:The pandas option affecting this argument has changed, as has thedefault value toFalse.

encodingstr, optional

A string representing the encoding to use in the output file,defaults to ‘utf-8’.

decimalstr, default ‘.’

Character recognized as decimal separator, e.g. ‘,’ in Europe.

multicolumnbool, default True

Use multicolumn to enhance MultiIndex columns.The default will be read from the config module, and is setas the optionstyler.sparse.columns.

Changed in version 2.0.0:The pandas option affecting this argument has changed.

multicolumn_formatstr, default ‘r’

The alignment for multicolumns, similar tocolumn_formatThe default will be read from the config module, and is set as the optionstyler.latex.multicol_align.

Changed in version 2.0.0:The pandas option affecting this argument has changed, as has thedefault value to “r”.

multirowbool, default True

Use multirow to enhance MultiIndex rows. Requires adding ausepackage{{multirow}} to your LaTeX preamble. Will printcentered labels (instead of top-aligned) across the containedrows, separating groups via clines. The default will be readfrom the pandas config module, and is set as the optionstyler.sparse.index.

Changed in version 2.0.0:The pandas option affecting this argument has changed, as has thedefault value toTrue.

captionstr or tuple, optional

Tuple (full_caption, short_caption),which results in\caption[short_caption]{{full_caption}};if a single string is passed, no short caption will be set.

labelstr, optional

The LaTeX label to be placed inside\label{{}} in the output.This is used with\ref{{}} in the main.tex file.

positionstr, optional

The LaTeX positional argument for tables, to be placed after\begin{{}} in the output.

Returns:
str or None

If buf is None, returns the result as a string. Otherwise returns None.

See also

io.formats.style.Styler.to_latex

Render a DataFrame to LaTeX with conditional formatting.

DataFrame.to_string

Render a DataFrame to a console-friendly tabular output.

DataFrame.to_html

Render a DataFrame as an HTML table.

Notes

As of v2.0.0 this method has changed to use the Styler implementation aspart ofStyler.to_latex() viajinja2 templating. This meansthatjinja2 is a requirement, and needs to be installed, for this methodto function. It is advised that users switch to using Styler, since thatimplementation is more frequently updated and contains much moreflexibility with the output.

Examples

Convert a general DataFrame to LaTeX with formatting:

>>>df=pd.DataFrame(dict(name=['Raphael','Donatello'],...age=[26,45],...height=[181.23,177.65]))>>>print(df.to_latex(index=False,...formatters={"name":str.upper},...float_format="{:.1f}".format,...))\begin{tabular}{lrr}\toprulename & age & height \\\midruleRAPHAEL & 26 & 181.2 \\DONATELLO & 45 & 177.7 \\\bottomrule\end{tabular}

[8]ページ先頭

©2009-2025 Movatter.jp