我有一個看起來像這樣的資料框,其中經度作為元組的第一個元素,緯度作為第二個元素:[1]:https ://i.stack.imgur.com/41zpN.jpg 我怎么能過濾資料框以僅選擇經度在范圍內 (19.07,19.82) 和緯度在范圍內 (-17.09,-20.37) 的行。謝謝!
uj5u.com熱心網友回復:
由于您沒有說明要過濾哪一列,因此這是我的解決方案,(longitude, latitude)僅用于在這些范圍內獲取對 column 1:
import pandas as pd
df = pd.DataFrame({'0': [(133.79, -72.91812133789062), (133.50, -72.95),
(133.22, -72.98)], '1': [(133.56, -72.97),
(133.28, -73.006), (19.59, -18.04)]})
lat_lower, lat_upper = 19.07, 19.82
long_lower, long_upper = -20.37, -17.09
col = df['1']
filtered = df.loc[(lat_lower < col.str[0]) & (col.str[0] < lat_upper) & (long_lower < col.str[1]) & (col.str[1] < long_upper)]
print(filtered)
輸出是:
0 1
(133.22, -72.98) (19.59, -18.04)
如您所見, column1僅具有您請求的范圍內的值,但 column0沒有。
如果您希望行的所有列都在這些范圍內,那么您應該對所有其他列重復相同的操作。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/465341.html
