python,根据几种条件过滤数据框

时间:2020-02-05 12:26:12

标签: python pandas dataframe

我有以下数据框:

enter image description here

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

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

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

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

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

1 个答案:

答案 0 :(得分:1)

语法应类似于:

function largeBitwiseOr(s1, s2) {
  const padded1 = s1.padStart(64, '0');
  const padded2 = s2.padStart(64, '0');
  const left1 = parseInt(padded1.substr(0, 32), 2);
  const left2 = parseInt(padded2.substr(0, 32), 2);
  const right1 = parseInt(padded1.substr(32, 32), 2);
  const right2 = parseInt(padded2.substr(32, 32), 2);
  const leftResult = (left1 | left2).toString(2);
  const rightResult = (right1 | right2).toString(2).padStart(32, '0');
  return leftResult + rightResult;
}

const string1 = "100000000000000000000000000000000000010";
const string2 = "010000000000000000000000000000000000001";
const string1or2 = largeBitwiseOr(string1, string2);
console.log(string1);
console.log(string2);
console.log(string1or2);

每个条件都用括号括起来的地方。

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