我有一个 .csv 文件,其中包含来自横断面的 xy 数据(此处为 .csv 文件)。该文件可以包含几十个样带(仅示例 4 个)。我想计算每个样带的高程变化,然后选择高程变化最高的样带。x y lines0 3.444 10.009 3.445 10.180 3.449 10.027 3.449 1...0 2.115 20.008 2.115 20.017 2.115 20.027 2.116 2 我试图用 pandas.dataframe.diff 计算变化,但我无法从中选择最高海拔变化。更新:我找到了一种计算 1 样带高度差的方法。现在的目标是通过不同的其他样带循环该脚本,并让它选择具有最大差异的样带。不知道如何从这个创建一个循环......import numpy as npimport matplotlib.pyplot as pltimport pandas as pdfrom scipy.signal import savgol_filter, find_peaks, find_peaks_cwtfrom pandas import read_csvimport csvdf = pd.read_csv('transect4.csv', delimiter=',', header=None, names=['x', 'y', 'lines'])df_1 = df ['lines'] == 1df1 = df[df_1]plt.plot(df1['x'], df1['y'], label='Original Topography')#apply a Savitzky-Golay filtersmooth = savgol_filter(df1.y.values, window_length = 351, polyorder = 5)#find the maximumspeaks_idx_max, _ = find_peaks(smooth, prominence = 0.01)#reciprocal, so mins will become maxsmooth_rec = 1/smooth#find the mins nowpeaks_idx_mins, _ = find_peaks(smooth_rec, prominence = 0.01)plt.xlabel('Distance')plt.ylabel('Height')plt.plot(df1['x'], smooth, label='Smoothed Topography')#plot themplt.scatter(df1.x.values[peaks_idx_max], smooth[peaks_idx_max], s = 55, c = 'green', label = 'Local Max Cusp')plt.scatter(df1.x.values[peaks_idx_mins], smooth[peaks_idx_mins], s = 55, c = 'black', label = 'Local Min Cusp')plt.legend(loc='upper left')plt.show()#Export to csvdf['Cusp_max']=Falsedf['Cusp_min']=Falsedf.loc[df1.x[peaks_idx_max].index, 'Cusp_max']=Truedf.loc[df1.x[peaks_idx_mins].index, 'Cusp_min']=Truedata=df[df['Cusp_max'] | df['Cusp_min']]data.to_csv(r'Cusp_total.csv')#Calculate height differencemy_data=pd.read_csv('Cusp_total.csv', delimiter=',', header=0, names=['ID', 'x', 'y', 'lines'])df_1 = df ['lines'] == 1df1 = df[df_1]df1_diff=pd.DataFrame(my_data)df1_diff['Diff_Cusps']=df1_diff['y'].diff(-1)理想情况下,脚本将从 .csv 文件中未知数量的样带中选择具有最高海拔变化的样带,然后将其导出到新的 .csv 文件。
1 回答
慕神8447489
TA贡献1780条经验 获得超1个赞
您需要groupby按列lines。
不确定这是否是您所说的海拔变化的意思,但这会给出每个组的海拔差异(max(y)-min(y)),其中组由所有行组成,每个组共享相同的“line”值代表一个这样的值。这应该可以帮助您解决逻辑中缺少的内容(抱歉不能投入更多时间)。
frame = pd.read_csv('transect4.csv', header=None, names=['x', 'y', 'lines'])
groups = frame.groupby('lines')
groups['y'].max() - groups['y'].min()
# Should give you max elevations of each group.
添加回答
举报
0/150
提交
取消