使用Pandas获取Eurostat数据的两类工具问题求助
Let's tackle your two tool issues one by one, with clear code examples and explanations tailored to your use case.
1. Fixing Data Filtering with pandas_datareader
The pandas_datareader Eurostat integration does support filtering—you just need to pass the right parameters to narrow down to TOTAL or NEW dwelling types. Here's how:
First, confirm the dimension codes for the prc_hpi_a dataset (you can look these up directly on the Eurostat website's dataset page). For housing type, the relevant dimension is indic_bt, with codes:
TOTAL: All dwellingsNEW: New dwellingsEXISTING: Existing dwellings (the default you're seeing)
Use the params argument in pandas_datareader.data.get_data_eurostat() to specify your filter:
import pandas_datareader.data as web # Fetch filtered data for TOTAL and NEW dwellings df = web.get_data_eurostat('prc_hpi_a', params={'indic_bt': ['TOTAL', 'NEW']}, start='2010-01-01') # Reset index to make dimensions more readable df = df.reset_index() print(df.head())
If you're unsure about dimension names, first fetch the dataset's metadata to list all available filters:
metadata = web.get_data_eurostat('prc_hpi_a', metadata=True) print(metadata['dimensions'])
2. Resolving pandasdmx Issues
The pandasdmx library has undergone breaking changes in recent versions, which is why old tutorials aren't working. Let's fix both your problems:
a. Fixing ImportError: cannot import name 'client'
The old client module was renamed to Request in newer versions. Update your import line to:
from pandasdmx import Request
Then initialize the Eurostat client like this:
estat = Request('ESTAT')
b. Fixing AttributeError: 'DataMessage' object has no attribute 'data'
Some Eurostat datasets store data in .data_set instead of .data within the DataMessage object. Here's a reliable way to extract and convert to a DataFrame:
# Fetch the prc_hicp_midx dataset resp = estat.data('prc_hicp_midx') # Handle different data storage attributes if hasattr(resp, 'data_set'): data = resp.data_set else: data = resp.data # Convert to Pandas DataFrame df = resp.write(data) print(df.head())
Alternatively, use the newer .to_pandas() method which automatically handles both cases:
df = resp.to_pandas()
For targeted filtering, specify keys when fetching data:
# Example: Filter prc_hicp_midx by country (DE, FR) and main indicator resp = estat.data('prc_hicp_midx', key={'geo': ['DE', 'FR'], 'indic': 'CP00'}) df = resp.to_pandas()
Additional Tips
- Always cross-reference the Eurostat dataset page for dimension codes and structure—this will speed up your filtering workflow.
- In Azure Notebooks, ensure you have the latest library versions installed:
!pip install --upgrade pandas_datareader pandasdmx
内容的提问来源于stack exchange,提问作者SPS

