XML-to-DataFrame ingestion
Load repeated XML records into a tabular DataFrame for analysis or transformation.
Pandas · Function
Reads XML from a path, URL, string, or file-like object into a DataFrame. XPath selects the element collection used for rows, while child elements and attributes become columns.
Pass XML text, a path, URL, or readable stream to parse selected XML nodes into DataFrame rows. The XPath must select a collection of elements; child elements and attributes become columns, with options for namespaces, types, dates, stylesheets, streaming, compression, and storage settings.
pandas.read_xml(path_or_buffer, *, xpath='./*', namespaces=None, elems_only=False, attrs_only=False, names=None, dtype=None, converters=None, parse_dates=None, encoding='utf-8', parser='lxml', stylesheet=None, iterparse=None, compression='infer', storage_options=None, dtype_backend=<no_default>)| Name | Type | Description |
|---|---|---|
path_or_buffer | str, path object, or file-like object | String path, path-like object, or file-like object with a read() method; URLs may also be supplied. |
xpath | str, optional | XPath selecting the collection of elements to parse; default is './*'. |
namespaces | dict, optional | Namespace prefixes and their URI mappings. |
elems_only | bool, optional, default False | Whether to parse only element values. |
attrs_only | bool, optional, default False | Whether to parse only attributes. |
names | list-like, optional | Column names for the resulting DataFrame. |
dtype | dict or callable, optional | Column dtype specification or conversion function. |
converters | dict, optional | Per-column conversion functions. |
parse_dates | list-like or dict, optional | Columns to parse as dates. |
encoding | str, optional, default 'utf-8' | Input encoding; default is UTF-8. |
parser | str, optional, default 'lxml' | XML parser to use; default is lxml. |
stylesheet | str or file-like object, optional | XSLT stylesheet used before parsing. |
DataFrame containing the parsed XML data.
import pandas as pd
xml = """<data><item id='1'><name>Ada</name><score>10</score></item><item id='2'><name>Linus</name><score>9</score></item></data>"""
df = pd.read_xml(xml, xpath="./item")
print(df)Parses each item element into a DataFrame row and includes its child elements and attribute as columns.
Load repeated XML records into a tabular DataFrame for analysis or transformation.
Select records nested under a particular XML path or namespace.
Process very large local XML files incrementally with lxml.
Apply an XSLT transformation before converting XML nodes to rows.
pandas.read_xml return and how is XML mapped to tabular data?read_xml parses an XML document and returns a pandas DataFrame. The selected XPath nodes become rows, while descendant elements and attributes are mapped to columns.
Follow-up: How would you handle a document with namespaces? Pass a prefix-to-URI dictionary through namespaces, then use those prefixes in the XPath expression.
xpath argument?xpath identifies the nodes to convert and defaults to ./*. It must select a collection of elements, not one individual element.
Follow-up: What happens when the XML uses a default namespace? Supply a namespace prefix mapping and prefix every tag in the XPath.
iterparse instead of ordinary XML parsing?iterparse supports incremental extraction for very large XML files, reducing memory use by avoiding a complete in-memory tree. It requires a local file path or file-like object and the lxml parser.
Follow-up: What should the iterparse mapping contain? Use repeating node names as keys and descendant element or attribute names as the associated lists.
pandas.DataFrame.to_xmlpandas.read_jsonpandas.read_htmlpandas.read_csv