start with geopandas

This commit is contained in:
Philip 2025-07-17 12:29:29 +02:00
parent 8563ee867e
commit bcc78638df
2 changed files with 73 additions and 1 deletions

View File

@ -41,8 +41,18 @@ print(positionen)
# Aufgabe 1: Bestellungen mit Kundennamen
# - Nur bestellungen von existierenden Kunden
order_customer = pd.merge(bestellungen, kunden, on="kunden_id", how="inner")
print(order_customer)
# Aufgabe 2: Vollständige Bestellübersicht (jede bestellung mit kunde und mit position mergen)
# Aufgabe 3: Kunden ohne Bestellungen finden [isna()]
step1 = pd.merge(bestellungen, kunden, on="kunden_id", how="left")
result = pd.merge(step1, positionen, on="bestell_id", how="left")
print(result)
# Aufgabe 3: Kunden ohne Bestellungen finden [.isna()]
kunden_ohne_best = pd.merge(kunden, bestellungen, on="kunden_id", how="left")
print(kunden_ohne_best)
print(kunden_ohne_best[kunden_ohne_best["bestell_id"].isna()][["kunden_id", "name"]])

62
src/T19_geopandas.py Normal file
View File

@ -0,0 +1,62 @@
# pip/conda install geopandas
import geopandas as gpd
import matplotlib.pyplot as plt
world = gpd.read_file("../data/110m_cultural.zip", layer="ne_110m_admin_0_countries")
print(list(world.columns))
print(world.iloc[0, -1])
print(world.iloc[0]["NAME"])
print()
print(world.crs)
# EPSG:4326 (latitude/longitude)
# epsg:3587 Mercator
w2 = world.to_crs("epsg:3587")
print(w2.iloc[0, -1])
print(world[["NAME", "CONTINENT", "POP_EST", "GDP_MD", "geometry"]].head())
# wie groß ist jedes Land?
world_proj_cylinder = world.to_crs("epsg:3587") # zylinder
world_proj_cylinder["area_sq_km"] = world_proj_cylinder.geometry.area / 1_000_000
print(world_proj_cylinder[["NAME", "area_sq_km"]].head(10))
cities = gpd.read_file("../data/110m_cultural.zip",
layer="ne_110m_populated_places")
print(cities[["NAME", "geometry"]].head())
print(len(cities))
fig, ax = plt.subplots()
world.plot(column="CONTINENT", legend=True, ax=ax)
cities.plot(ax=ax, color="red")
plt.show()
# Nachbar von Land X suchen
def find_neighbouring_countries(df, country_name):
target = df[df["NAME"] == country_name]
if len(target) == 0:
print(f"{country_name} not found")
return []
# sjoin
neighbours = []
for idx, country in df.iterrows():
if country["NAME"] == country_name:
continue
if target.geometry.iloc[0].touches(country.geometry):
neighbours.append(country["NAME"])
return neighbours
res = find_neighbouring_countries(world, "Spain")
print(res)