DataFrame.to_xml(path_or_buffer=None, index=True, root_name='data', row_name='row', na_rep=None, attr_cols=None, elem_cols=None, namespaces=None, prefix=None, encoding='utf-8', xml_declaration=True, pretty_print=True, parser='lxml', stylesheet=None, compression='infer', storage_options=None)[source]

Render a DataFrame to an XML document.

New in version 1.3.0.

path_or_bufferstr, path object or file-like object, optional

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

indexbool, default True

Whether to include index in XML document.

root_namestr, default ‘data’

The name of root element in XML document.

row_namestr, default ‘row’

The name of row element in XML document.

na_repstr, optional

Missing data representation.

attr_colslist-like, optional

List of columns to write as attributes in row element. Hierarchical columns will be flattened with underscore delimiting the different levels.

elem_colslist-like, optional

List of columns to write as children in row element. By default, all columns output as children of row element. Hierarchical columns will be flattened with underscore delimiting the different levels.

namespacesdict, optional

All namespaces to be defined in root element. Keys of dict should be prefix names and values of dict corresponding URIs. Default namespaces should be given empty string key. For example,

namespaces = {"": ""}
prefixstr, optional

Namespace prefix to be used for every element and/or attribute in document. This should be one of the keys in namespaces dict.

encodingstr, default ‘utf-8’

Encoding of the resulting document.

xml_declarationbool, default True

Whether to include the XML declaration at start of document.

pretty_printbool, default True

Whether output should be pretty printed with indentation and line breaks.

parser{‘lxml’,’etree’}, default ‘lxml’

Parser module to use for building of tree. Only ‘lxml’ and ‘etree’ are supported. With ‘lxml’, the ability to use XSLT stylesheet is supported.

stylesheetstr, path object or file-like object, optional

A URL, file-like object, or a raw string containing an XSLT script used to transform the raw XML output. Script should use layout of elements and attributes from original output. This argument requires lxml to be installed. Only XSLT 1.0 scripts and not later versions is currently supported.

compression{‘infer’, ‘gzip’, ‘bz2’, ‘zip’, ‘xz’, None}, default ‘infer’

For on-the-fly decompression of on-disk data. If ‘infer’, then use gzip, bz2, zip or xz if path_or_buffer is a string ending in ‘.gz’, ‘.bz2’, ‘.zip’, or ‘xz’, respectively, and no decompression otherwise. If using ‘zip’, the ZIP file must contain only one data file to be read in. Set to None for no decompression.

storage_optionsdict, optional

Extra options that make sense for a particular storage connection, e.g. host, port, username, password, etc. For HTTP(S) URLs the key-value pairs are forwarded to urllib as header options. For other URLs (e.g. starting with “s3://”, and “gcs://”) the key-value pairs are forwarded to fsspec. Please see fsspec and urllib for more details.

None or str

If io is None, returns the resulting XML format as a string. Otherwise returns None.

See also


Convert the pandas object to a JSON string.


Convert DataFrame to a html.


>>> df = pd.DataFrame({'shape': ['square', 'circle', 'triangle'],
...                    'degrees': [360, 360, 180],
...                    'sides': [4, np.nan, 3]})
>>> df.to_xml()  
<?xml version='1.0' encoding='utf-8'?>
>>> df.to_xml(attr_cols=[
...           'index', 'shape', 'degrees', 'sides'
...           ])  
<?xml version='1.0' encoding='utf-8'?>
  <row index="0" shape="square" degrees="360" sides="4.0"/>
  <row index="1" shape="circle" degrees="360"/>
  <row index="2" shape="triangle" degrees="180" sides="3.0"/>
>>> df.to_xml(namespaces={"doc": ""},
...           prefix="doc")  
<?xml version='1.0' encoding='utf-8'?>
<doc:data xmlns:doc="">