简体   繁体   中英

Get the minimal difference values between df columns

i have a df that looks like this:

         snr        freq         snr  ...         snr        freq    freq_ref
0     111.796861  400.003168  116.805099  ...  123.952201  400.046262  400.000000
1     111.800587  400.010109  117.194605  ...  124.033467  400.083761  400.033333
2     111.636656  400.012101  117.654265  ...  124.155229  400.117228  400.066667
3     111.839271  400.031985  118.009703  ...  124.208280  400.192227  400.100000
4     112.162853  400.096895  118.196040  ...  124.055698  400.218755  400.133333

i want to iterate over the df and subtract each freq columns from the freq_ref column, get the minimal value of each subtraction, and get the corresponding snr value of that freq.

any ideas?

thank you

You could select the columns by

cols = df.iloc[:, 1::2].rsub(df.freq_ref, axis=0).to_numpy().argmin(axis=1)

and then build the corresponding dataframe by

values = [[df.iat[i, j], df.iat[i, j + 1], df.iat[i, -1]]
          for i, j in zip(df.index, cols * 2)]
df_min = pd.DataFrame(values, columns=["snr", "freq", "freq_ref"], index=df.index)

Or if you want to keep the result of the subtraction:

df_sub = df.iloc[:, 1::2].rsub(df.freq_ref, axis=0)
cols = df_sub.to_numpy().argmin(axis=1)
values = [[df.iat[i, 2 * j], df_sub.iat[i, j], df.iat[i, -1]]
          for i, j in zip(df.index, cols)]
df_min = pd.DataFrame(values, columns=["snr", "freq_sub", "freq_ref"], index=df.index)

Here's a pure NumPy alternative which is a bit more compact:

mat = df.to_numpy()
mat_sub = mat[:, [-1]] - mat[:, 1::2]
idx = mat_sub.argmin(axis=1)
df_result = pd.DataFrame({
    "snr": np.choose(idx, mat[:, 0:-1:2].T),
    "freq_sub": np.choose(idx, mat_sub.T),
    "freq_ref": df.freq_ref
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM