Python tutorial: European fuel prices
This tutorial fetches current diesel prices for a city in France, Spain or Italy with Python and summarises them with pandas.
Install
pip install apify-client pandas
Fetch prices
import os
import pandas as pd
from apify_client import ApifyClient
client = ApifyClient(os.environ["APIFY_TOKEN"])
run = client.actor("sste/official-fuel-prices").call(run_input={
"latitude": 40.4168, "longitude": -3.7038, # Madrid
"radiusKm": 15, "fuelTypes": ["diesel"], "maxResults": 200,
})
df = pd.DataFrame(client.dataset(run["defaultDatasetId"]).list_items().items)
print(df[["price", "brand", "address", "distanceKm"]].head(10))
Summarise by brand
summary = (df.groupby("brand")["price"]
.agg(["count", "min", "median"])
.sort_values("median"))
print(summary.head(10))
Notes
- Use
countries: ["FR", "ES", "IT"]without coordinates to fetch whole countries (mind the per-row price). priceUpdatedAtis an ISO timestamp with time zone; parse it withpd.to_datetime(df["priceUpdatedAt"], utc=True).- Italian rows have self-service and attended prices; filter with
df[df.serviceType == "self"].