On continuation from my first thread here Code that creates variables, and increments its suffix
So I already have a list called polygons
I am now trying to automate the code for the 10000 polygons (code for two polygons shown below as example). This code works fine if I were to do it manually for 10000 polygons but I am having issues automating it for 10K polygons
### Polygon 1
def checkPolygon1(row):
point = Point((row['X']), (row['Y']))
return polygon1.contains(point)
#Apply fn to each row of dataframe (datacopy)
datacopy.apply(checkPolygon1, axis=1)
#Create new variable InPolygon1 in data if X, Y in shapely file
datacopy['InPolygon1'] = datacopy.apply(checkPolygon1, axis=1)
### Polygon 2
def checkPolygon2(row):
point = Point((row['X']), (row['Y']))
return polygon2.contains(point)
datacopy.apply(checkPolygon2, axis=1)
#Create new variable InPolygon2 in data if X, Y in shapely file
datacopy['InPolygon2'] = datacopy.apply(checkPolygon2, axis=1)
The idea is to have a dataframe with 10000 variables so InPolygon1, InPolygon2...InPolygon10000
Some of my attempts: So to start with, I am trying to create another list that takes
polygons[0].contains(point),
polygons[1].contains(point)...polygons[10000].contains(point)
But I am not able to correctly create it.
def checkPolygon(row):
point = Point((row['X']), (row['Y']))
return point
nextpolygons = []
def check():
for i in range(0,2): #actual range(0,10000)
nextpolygons.append(polygons[i].contains(checkPolygon(row)))
check() #NameError: name 'row' is not defined
My idea is that after this list is correctly defined, I will try to automate the portions below:
datacopy.apply(checkPolygon1, axis=1)
datacopy['InPolygon1'] = datacopy.apply(checkPolygon1, axis=1)
Any ideas on this would be highly appreciated!