I have a geojson document 'streets' with a column "geometry" filled with data of type shapely Point. I have another geojson document 'quartiers' with a column 'geometry" with data of type shapely Polygon and in same document I have another column called "l_qu". What I am trying to achieve is looping through all streets['geometry] with a lambda in quartiers['geometry'] and assign the corresponding 'l_qu' match value in a new column 'matchquartier' in 'streets'. There are 150K lines in streets (the adresses) and 80 lines in quartiers (the neighboorhoods) The lambda for I built is :
def checkquartier(point):
return quartiers[quartiers["geometry"].apply(point.within)]["l_qu"]
When running some test fo the lambda, say: checkquartier(streets['geometry'][19]) , no issue, it returns:
19 La Chapelle Name: l_qu, dtype: object
which is the expected result. The problem is when I try to do it for my whole doc with
streets["matchquartier"]=streets['geometry'].apply(lambda x: checkquartier(x))
It returns the error: ValueError: Wrong number of items passed 80, placement implies 1
I know this is a broader pandas issue but the other fixes/answers I found on SO do not work in this case so wondering if there is something from the GeoPandas side that I am doing wrong ? Thanks
import pandas as pd
import geopandas
import os
import shapely
from shapely.geometry import Polygon, LineString, Point,box
import matplotlib.pyplot as plt
from shapely import wkt
quartiers = geopandas.read_file("https://parisdata.opendatasoft.com/explore/dataset/quartier_paris/download/?format=geojson&timezone=Europe/Berlin&lang=fr")
streets=geopandas.read_file("https://opendata.paris.fr/explore/dataset/adresse_paris/download/?format=geojson&timezone=Europe/Berlin&lang=fr")
def checkquartier(point):
return quartiers[quartiers["geometry"].apply(point.within)]["l_qu"]
streets["matchquartier"]=streets['geometry'].apply(lambda x: checkquartier(x))