![](/img/trans.png)
[英]Add new column to a pandas dataframe based on conditional statement of existing column
[英]Generate new column in pandas dataframe based on conditional statement
我有 2 個熊貓數據框。 第一個包含站點的經緯度信息,只有 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
第二個包含列“多邊形”(勻稱多邊形格式)並有 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, ...
我的目標是:
我該怎么辦:
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.
所以,最后我想再多列一列“多邊形中的站數”,例如:
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')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.