xorbits.pandas.DataFrame.to_latex#

DataFrame.to_latex(buf: FilePath | WriteBuffer[str] | None = None, columns: Sequence[Hashable] | None = None, header: bool_t | list[str] = True, index: bool_t = True, na_rep: str = 'NaN', formatters: FormattersType | None = None, float_format: FloatFormatType | None = None, sparsify: bool_t | None = None, index_names: bool_t = True, bold_rows: bool_t = False, column_format: str | None = None, longtable: bool_t | None = None, escape: bool_t | None = None, encoding: str | None = None, decimal: str = '.', multicolumn: bool_t | None = None, multicolumn_format: str | None = None, multirow: bool_t | None = None, caption: str | tuple[str, str] | None = None, label: str | None = None, position: str | None = None) str | None#

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}}.

在 1.2.0(pandas) 版更改: Added position argument, changed meaning of caption argument.

在 2.0.0(pandas) 版更改: Refactored to use the Styler implementation via jinja2 templating.

参数
  • buf (str, Path or StringIO-like, optional, default None) – Buffer to write to. If None, the output is returned as a string.

  • columns (list of label, optional) – The subset of columns to write. Writes all columns by default.

  • header (bool 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.

  • index (bool, default True) – Write row names (index).

  • na_rep (str, default 'NaN') – Missing data representation.

  • formatters (list of functions or dict of {{str: function}}, optional) – Formatter functions to apply to columns’ elements by position or name. The result of each function must be a unicode string. List must be of length equal to the number of columns.

  • float_format (one-parameter function or str, optional, default None) – Formatter for floating point numbers. For example float_format="%.2f" and float_format="{{:0.2f}}".format will both result in 0.1234 being formatted as 0.12.

  • sparsify (bool, optional) – Set to False for a DataFrame with a hierarchical index to print every multiindex key at each row. By default, the value will be read from the config module.

  • index_names (bool, default True) – Prints the names of the indexes.

  • bold_rows (bool, default False) – Make the row labels bold in the output.

  • column_format (str, optional) – 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’.

  • longtable (bool, optional) –

    Use a longtable environment instead of tabular. Requires adding a usepackage{{longtable}} to your LaTeX preamble. By default, the value will be read from the pandas config module, and set to True if the option styler.latex.environment is “longtable”.

    在 2.0.0(pandas) 版更改: The pandas option affecting this argument has changed.

  • escape (bool, optional) –

    By default, the value will be read from the pandas config module and set to True if the option styler.format.escape is “latex”. When set to False prevents from escaping latex special characters in column names.

    在 2.0.0(pandas) 版更改: The pandas option affecting this argument has changed, as has the default value to False.

  • encoding (str, optional) – A string representing the encoding to use in the output file, defaults to ‘utf-8’.

  • decimal (str, default '.') – Character recognized as decimal separator, e.g. ‘,’ in Europe.

  • multicolumn (bool, default True) –

    Use multicolumn to enhance MultiIndex columns. The default will be read from the config module, and is set as the option styler.sparse.columns.

    在 2.0.0(pandas) 版更改: The pandas option affecting this argument has changed.

  • multicolumn_format (str, default 'r') –

    The alignment for multicolumns, similar to column_format The default will be read from the config module, and is set as the option styler.latex.multicol_align.

    在 2.0.0(pandas) 版更改: The pandas option affecting this argument has changed, as has the default value to “r”.

  • multirow (bool, default True) –

    Use multirow to enhance MultiIndex rows. Requires adding a usepackage{{multirow}} to your LaTeX preamble. Will print centered labels (instead of top-aligned) across the contained rows, separating groups via clines. The default will be read from the pandas config module, and is set as the option styler.sparse.index.

    在 2.0.0(pandas) 版更改: The pandas option affecting this argument has changed, as has the default value to True.

  • caption (str 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.

    在 1.2.0(pandas) 版更改: Optionally allow caption to be a tuple (full_caption, short_caption).

  • label (str, optional) – The LaTeX label to be placed inside \label{{}} in the output. This is used with \ref{{}} in the main .tex file.

  • position (str, optional) –

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

    1.2.0(pandas) 新版功能.

返回

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

返回类型

str or None

参见

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.

提示

As of v2.0.0 this method has changed to use the Styler implementation as part of Styler.to_latex() via jinja2 templating. This means that jinja2 is a requirement, and needs to be installed, for this method to function. It is advised that users switch to using Styler, since that implementation is more frequently updated and contains much more flexibility with the output.

实际案例

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}
\toprule
name & age & height \\
\midrule
RAPHAEL & 26 & 181.2 \\
DONATELLO & 45 & 177.7 \\
\bottomrule
\end{tabular}

警告

This method has not been implemented yet. Xorbits will try to execute it with pandas.

This docstring was copied from pandas.core.frame.DataFrame.