Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Pandas · Function

pd.read_xml

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.

Since
1.3.0

How it works

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.

Syntax

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>)

Parameters

NameTypeDescription
path_or_bufferstr, path object, or file-like objectString path, path-like object, or file-like object with a read() method; URLs may also be supplied.
xpathstr, optionalXPath selecting the collection of elements to parse; default is './*'.
namespacesdict, optionalNamespace prefixes and their URI mappings.
elems_onlybool, optional, default FalseWhether to parse only element values.
attrs_onlybool, optional, default FalseWhether to parse only attributes.
nameslist-like, optionalColumn names for the resulting DataFrame.
dtypedict or callable, optionalColumn dtype specification or conversion function.
convertersdict, optionalPer-column conversion functions.
parse_dateslist-like or dict, optionalColumns to parse as dates.
encodingstr, optional, default 'utf-8'Input encoding; default is UTF-8.
parserstr, optional, default 'lxml'XML parser to use; default is lxml.
stylesheetstr or file-like object, optionalXSLT stylesheet used before parsing.

Returns

DataFrame containing the parsed XML data.

Key points

  • The function converts XML content into a pandas DataFrame.
  • The input may be a path, readable object, or URL using supported schemes such as HTTP, FTP, S3, or file.
  • Set attrs_only to build columns from attributes rather than element contents.

Common mistakes

  • Set xpath to select repeated elements rather than one element; a single selected node is not the expected input shape.

Example: Read repeated XML elements into a DataFrame

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.

When to use it

XML-to-DataFrame ingestion

Load repeated XML records into a tabular DataFrame for analysis or transformation.

Targeted XPath extraction

Select records nested under a particular XML path or namespace.

Memory-conscious parsing

Process very large local XML files incrementally with lxml.

Pre-parse XML transformation

Apply an XSLT transformation before converting XML nodes to rows.

Interview questions

QWhat does 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.

QWhat constraints apply to the 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.

QWhen would you use 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_xml
  • pandas.read_json
  • pandas.read_html
  • pandas.read_csv

Source: pandas.read_xml — pandas 3.0.6 documentation. Checked 2026-10-15.