gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:37:56 25 4
gpt4 key购买 nike

我有以下数据框:

enter image description here

我想根据以下条件过滤它:

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

GDT 1 和 GDT 2 之间的距离 >= 2 * UAV 和 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: []

最佳答案

语法应如下所示:

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用于单独的条件以简化过滤器语句。

关于python,根据几个条件过滤数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60075812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com