I've got a dataframe that looks like this that has longitude as a first element of a tuple, and latitude as a second element: [1]: https://i.stack.imgur.com/41zpN.jpg How can I filter dataframe to select only rows where longitude would be in range (19.07,19.82) and latitude in range (-17.09,-20.37). Thanks!
CodePudding user response:
As you didn't state which column you want to filter, here is my solution to get (longitude, latitude)
pairs in those ranges only for 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)
output is:
0 1
(133.22, -72.98) (19.59, -18.04)
Here as you can see, column 1
has only values in the range you requested, but column 0
does not.
If you want rows with all its columns in those ranges, then you should repeat the same for all other columns.