减少从大型 pandas 数据框中定位四个 XYZ 坐标的时间

问题描述 投票:0回答:0

我们正在尝试将 3D 网格上的四个点存储在数据框中。每个点都有 x、y、z 值以及 y 和 z 方向上相应的力。我们想要的第一个点位于给定半径和角度 0 处,下一个点距该点 180 度,第三个点与第二个点位于相同的 y,z 坐标,但具有其他 x 值(有只有两个可能的 x 值),最后一个点与第一个点位于同一 y,z 点,但具有另一个 x。

数据框包含 5 列; X 位置 (mm)、Y 位置 (mm) 和 Z 位置 (mm)、Force_y_direction、Force_z_direction。这些坐标是从 .csv 文件读取并存储在 pandas 数据框中;

def link_csv_files():
    # Read xyz coordinates from csv file
    df_x = pd.read_csv('x-ees-data.csv', engine='python')

    create_nodes(df_x, df_y, df_z)

def create_nodes(df_x, df_y, df_z):
    # Copy the common columns into the df_combined dataframe (Node Number, X Location, Y Location, Z Location)
    df_combined['Node Number'] = df_x['Node Number']
    df_combined['X Location (mm)'] = df_x['X Location (mm)']
    df_combined['Y Location (mm)'] = df_x['Y Location (mm)']
    df_combined['Z Location (mm)'] = df_x['Z Location (mm)']

    # Iterate over the columns for the y-directional force
    for column in df_y.columns[4:]:
        # Copy the column values to the combined DataFrame
        df_combined['Y Elastic Strain (mm/mm)'] = df_y[column]

    # Iterate over the columns of the z-directional force
    for column in df_z.columns[4:]:
        # Copy the column values to the combined DataFrame
        df_combined['Z Elastic Strain (mm/mm)'] = df_z[column]

大约有500,000点

这些点落在圆上的不同半径上,并且它们位于半径周围每隔 11.25 度的位置。 y 是长度,z 是高度,x 是深度。 3D point, y-z view

我们想要找到将在方程中使用的两组点:第一组点具有相同的深度,但相距 180 度。第二组点位于相同的 y,z 位置,但位于另一侧(有 2 个深度值)。

问题是目前需要 11 分钟才能找到一个半径的四个点。我们想减少这个时间。我们可以使用任何库。

这是我当前的实现:

# Function to find the combination of 4 points
# df_points: The dataframe containing all node locations
# points_arr: An array of every points along 0-180 degrees
def find_point_combinations(df_points, points_arr):
    
    # Array to store the two sets of points.
    voltage = []

    for n in range(0, len(points_arr)):
        point1 = points_arr[n]
        point3 = point1

        #Finding point3 
        for pt in range(0, len(df_points)):
            if df_points['Y Location (mm)'].loc[pt] == -1 * point1[2] and df_points['Z Location (mm)'].loc[pt] == -1 * point1[3]:
                if df_points['X Location (mm)'].loc[pt] == point1[1]:
                    point3 = df_points.loc[pt]
                    break  

        #Finding point4, which is on the other side
        for pt in range(0, len(df_points)):
            if df_points['Y Location (mm)'].loc[pt] == point3[2] and df_points['Z Location (mm)'].loc[pt] == point3[3]:
                if df_points['X Location (mm)'].loc[pt] != point3[1]:
                    point4 = df_points.loc[pt]
                    break

        #Finding point2
        for pt in range(0, len(df_points)):
            if df_points['Y Location (mm)'].loc[pt] == point1[2] and df_points['Z Location (mm)'].loc[pt] == point1[3]:
                if df_points['X Location (mm)'].loc[pt] != point1[1]:
                    point2 = df_points.loc[pt]
                    break

       voltage.append(point1, point2, point3, point4)
       return voltage
python pandas performance logic
© www.soinside.com 2019 - 2024. All rights reserved.