根据条件语句在 pandas 数据框中生成新列

Generate new column in pandas dataframe based on conditional statement

我有 2 个 pandas 数据帧。第一个包含站点的经纬度信息,只有3行:

    stat_id    stat_lon     stat_lat
0   db_695203   9.444328    54.787590
1   db_699007   9.438629    54.789577
2   db_695442   9.445865    54.786215

第二个包含第 'Polygon' 列(形状多边形格式)并且有 20 行:

0     POLYGON ((9.444721146384639 54.78805404001241,...
1     POLYGON ((9.429828147969117 54.79003403977831,... 
2     POLYGON ((9.429153147576411 54.78516304109078,...
.......................................................
18    POLYGON ((9.417355147148637 54.79108504035977,...
19    POLYGON ((9.44272277037326 54.79218198146992, ...

我的目标是:

  1. 检查点(她的坐标站)是否在多边形中(没问题)
  2. 计算一个多边形中的站数(这就是问题所在)

我要做什么:

for j in range(len(piece_clean_data)): #it's a df which contains polygons
P = shapely.wkt.loads(piece_clean_data.iloc[j,87]) #i convert string to Polygon
for i in range(len(three_stations)): #df with 3 stations
    p1 = Point(three_stations.iloc[i,1], three_stations.iloc[i,2]) #station coordinates
    st = P.contains(p1) #the answer is "True/False" - here i check, whether the point is in polygon or not
    if st == 'True': #and here I don't have any idea.

所以,最后我想多一列"Number of stations in Polygon",例如:

    0     POLYGON ((9.444721146384639 54.78805404001241,...   0
    1     POLYGON ((9.429828147969117 54.79003403977831,...   0 
    2     POLYGON ((9.429153147576411 54.78516304109078,...   1

有什么想法吗?非常感谢!

首先定义一个函数,给定一行迭代站点列表并计算站点是否包含在多边形中。然后将此函数应用于多边形列表 DataFrame 的每一行。

def num_stations(polygon):
  """Count the number of stations that are within a given polygon."""
  num = 0
  for _, station in three_stations.iterrows():
    p = Point(station['stat_lon'], station['stat_lat'])
    if polygon.contains(p):
      num += 1
  return num

piece_clean_data['station_counts'] = piece_clean_data.apply(num_stations, axis='columns')