2

I have the following dataframe:

enter image description here

I want to filter it based on the following the conditions:

Angle Created = range(87 - 92)

Distance between GDT 1 and GDT 2 >= 2 * Distance between UAV and MidPoint

So far I tried this ( the last method):

class DataFrameToGeneratedList:

    def __init__(self, lat_lon_list=None, lat_list=None, lon_list=None):
        if lon_list is None:
            lon_list = []
        if lat_list is None:
            lat_list = []
        if lat_lon_list is None:
            lat_lon_list = []
        self.lat_lon_list = lat_lon_list
        self.lat_list = lat_list
        self.lon_list = lon_list

    # Some unrelated methods here...


    def create_points_df(self):
        # Convert points list to xy coordinates.
        xy_lat_lon_list = [convert_to_xy(l_xy) for l_xy in self.lat_lon_list]
        # Midpoint between gdt1 and every point in xy.
        midpoints_xy = [get_midpoint(gdt1_xy, point) for point in xy_lat_lon_list]
        # Converted midpoints from xy to GeoPoints.
        midpoints = [convert_to_lat_lon(xy_point) for xy_point in midpoints_xy]
        # Distance from gdt 1 to every point.
        distances = [get_distances(gdt1, geo_point) for geo_point in self.lat_lon_list]
        # List of angles for every point in lat_lon_list.
        angled_list = [angle_between_points(arrayed_gdt1, arrayed_uav, point) for point in xy_lat_lon_list]
        # Get distance from uav to every midpoint created.
        midpoints_to_uav = [get_distances(uav, midpoint) for midpoint in midpoints]

        data_dict = {
            'Latitude': self.lat_list,
            'Longitude': self.lon_list,
            'Angle Created': angled_list,
            'Point In XY': xy_lat_lon_list,
            'MidPoint of GDT 1 and GDT 2': midpoints_xy,
            'Distance between GDT 1 and GDT 2': distances,
            'Distance between UAV and MidPoint': midpoints_to_uav
        }
        unfilterd_df = pd.DataFrame(data_dict)
        print(unfilterd_df)
        return unfilterd_df

    def filter_df_results(self, finished_df):
        assert isinstance(finished_df, pd.DataFrame)
        finished_df = finished_df
        finished_df = (finished_df[(finished_df['Angle Created'] >= 88) & (finished_df['Angle Created'] <= 95) &
                                   (finished_df['Distance between GDT 1 and GDT 2']) >= (
                                               2 * finished_df['Distance between UAV and MidPoint'])])
        print(finished_df)


if __name__ == '__main__':
    a = DataFrameToGeneratedList()
    a.generate_points_list(a.generate_pd())
    df = a.create_points_df()
    a.filter_df_results(finished_df=df)

The output of this code is an empty database with no errors.

Empty DataFrame
Columns: [Latitude, Longitude, Angle Created, Point In XY, MidPoint of GDT 1 and GDT 2, Distance between GDT 1 and GDT 2, Distance between UAV and MidPoint]
Index: []

1 Answers1

2

The syntax should look like:

finished_df = (
    finished_df[
            (finished_df['Angle Created'] >= 88) &
            (finished_df['Angle Created'] <= 95) & 
            (finished_df['Distance between GDT 1 and GDT 2'] >= (2 * finished_df['Distance between UAV and MidPoint']))]
    )

Where each condition is enclosed in parentheses.

You could consider creating masks for separate conditions to simplify the filter statement.

David Foster
  • 447
  • 4
  • 16