Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.DataFrame.to_xml#

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.

Added in version 1.3.0.

Parameters:
path_or_bufferstr, path object, file-like object, or None, default None

String, path object (implementingos.PathLike[str]), or file-likeobject implementing awrite() function. If None, the result is returnedas 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 underscoredelimiting 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. Hierarchicalcolumns will be flattened with underscore delimiting thedifferent levels.

namespacesdict, optional

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

namespaces={"":"https://example.com"}
prefixstr, optional

Namespace prefix to be used for every element and/or attributein document. This should be one of the keys innamespacesdict.

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 andline 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 XSLTstylesheet is supported.

stylesheetstr, path object or file-like object, optional

A URL, file-like object, or a raw string containing an XSLTscript used to transform the raw XML output. Script should uselayout of elements and attributes from original output. Thisargument requireslxml to be installed. Only XSLT 1.0scripts and not later versions is currently supported.

compressionstr or dict, default ‘infer’

For on-the-fly compression of the output data. If ‘infer’ and ‘path_or_buffer’ ispath-like, then detect compression from the following extensions: ‘.gz’,‘.bz2’, ‘.zip’, ‘.xz’, ‘.zst’, ‘.tar’, ‘.tar.gz’, ‘.tar.xz’ or ‘.tar.bz2’(otherwise no compression).Set toNone for no compression.Can also be a dict with key'method' setto one of {'zip','gzip','bz2','zstd','xz','tar'} andother key-value pairs are forwarded tozipfile.ZipFile,gzip.GzipFile,bz2.BZ2File,zstandard.ZstdCompressor,lzma.LZMAFile ortarfile.TarFile, respectively.As an example, the following could be passed for faster compression and to createa reproducible gzip archive:compression={'method':'gzip','compresslevel':1,'mtime':1}.

Added in version 1.5.0:Added support for.tar files.

Changed in version 1.4.0:Zstandard support.

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 pairsare forwarded tourllib.request.Request as header options. For otherURLs (e.g. starting with “s3://”, and “gcs://”) the key-value pairs areforwarded tofsspec.open. Please seefsspec andurllib for moredetails, and for more examples on storage options referhere.

Returns:
None or str

Ifio is None, returns the resulting XML format as astring. Otherwise returns None.

See also

to_json

Convert the pandas object to a JSON string.

to_html

Convert DataFrame to a html.

Examples

>>>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'?><data>  <row>    <index>0</index>    <shape>square</shape>    <degrees>360</degrees>    <sides>4.0</sides>  </row>  <row>    <index>1</index>    <shape>circle</shape>    <degrees>360</degrees>    <sides/>  </row>  <row>    <index>2</index>    <shape>triangle</shape>    <degrees>180</degrees>    <sides>3.0</sides>  </row></data>
>>>df.to_xml(attr_cols=[...'index','shape','degrees','sides'...])<?xml version='1.0' encoding='utf-8'?><data>  <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"/></data>
>>>df.to_xml(namespaces={"doc":"https://example.com"},...prefix="doc")<?xml version='1.0' encoding='utf-8'?><doc:data xmlns:doc="https://example.com">  <doc:row>    <doc:index>0</doc:index>    <doc:shape>square</doc:shape>    <doc:degrees>360</doc:degrees>    <doc:sides>4.0</doc:sides>  </doc:row>  <doc:row>    <doc:index>1</doc:index>    <doc:shape>circle</doc:shape>    <doc:degrees>360</doc:degrees>    <doc:sides/>  </doc:row>  <doc:row>    <doc:index>2</doc:index>    <doc:shape>triangle</doc:shape>    <doc:degrees>180</doc:degrees>    <doc:sides>3.0</doc:sides>  </doc:row></doc:data>

[8]ページ先頭

©2009-2025 Movatter.jp