1

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!

yuliya
  • 55
  • 5
  • 1
    if you want to get help here please have a quick look in [how to ask](https://stackoverflow.com/help/how-to-ask). What have you tried so far? provide a [minimal reproducable example](https://stackoverflow.com/help/minimal-reproducible-example) – Rabinzel Apr 26 '22 at 18:42
  • 1
    You want to do this for all columns? And can you plz provide your expected output – AlirezaAsadi Apr 26 '22 at 18:42
  • `df.explode` might be a place to start looking. but the many columns there seem a bit overwhelming... – scotscotmcc Apr 26 '22 at 18:46
  • Could you provide a sample of the data in a code-based format? [This](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) is worth a read. – BrokenBenchmark Apr 26 '22 at 19:22

1 Answers1

0

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.

AlirezaAsadi
  • 793
  • 2
  • 6
  • 21