【问题标题】:python, filter dataframe based on several conditionpython,根据几个条件过滤数据框
【发布时间】:2020-02-05 12:26:12
【问题描述】:

我有以下数据框:

我想根据以下条件对其进行过滤:

创建的角度 = 范围 (87 - 92)

GDT 1 和 GDT 2 之间的距离 >= 2 * 无人机和 MidPoint 之间的距离

到目前为止,我尝试了这个(最后一种方法):

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)

这段代码的输出是一个没有错误的空数据库。

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: []

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    语法应该是这样的:

    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']))]
        )
    

    每个条件都用括号括起来。

    您可以考虑为单独的条件创建masks 以简化过滤器语句。

    【讨论】:

      猜你喜欢
      • 2020-08-29
      • 1970-01-01
      • 1970-01-01
      • 2012-12-21
      • 1970-01-01
      • 1970-01-01
      • 2018-10-08
      • 1970-01-01
      相关资源
      最近更新 更多