Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find nearest value from multiple columns and add to a new column in Python

I have the following dataframe:

import pandas as pd
import numpy as np
data = {
    "index": [1, 2, 3, 4, 5],
    "A": [11, 17, 5, 9, 10],
    "B": [8, 6, 16, 17, 9],
    "C": [10, 17, 12, 13, 15],
    "target": [12, 13, 8, 6, 12]
}
df = pd.DataFrame.from_dict(data)
print(df)

I would like to find nearest values for column target in column A, B and C, and put those values into column result. As far as I know, I need to use abs() and argmin() function. Here is the output I expected:

     index   A      B     C    target  result
0      1     11     8    10      12      11
1      2     17     6    17      13      17
2      3     5     16    12       8       5
3      4     9     17    13       6       9
4      5     10     9    15      12      10

Here is the solution and links what i have found from stackoverflow which may help:

(df.assign(closest=df.apply(lambda x: x.abs().argmin(), axis='columns'))
 .apply(lambda x: x[x['target']], axis='columns'))

Identifying closest value in a column for each filter using Pandas https://codereview.stackexchange.com/questions/204549/lookup-closest-value-in-pandas-dataframe

like image 378
ah bon Avatar asked Dec 24 '22 02:12

ah bon


1 Answers

Subtract "target" from the other columns, use idxmin to get the column of the minimum difference, followed by a lookup:

idx = df.drop(['index', 'target'], 1).sub(df.target, axis=0).abs().idxmin(1)
df['result'] = df.lookup(df.index, idx)
df
   index   A   B   C  target  result
0      1  11   8  10      12      11
1      2  17   6  17      13      17
2      3   5  16  12       8       5
3      4   9  17  13       6       9
4      5  10   9  15      12      10

General solution handling string columns and NaNs (along with your requirement of replacing NaN values in target with value in "v1"):

df2 = df.select_dtypes(include=[np.number])
idx = df2.drop(['index', 'target'], 1).sub(df2.target, axis=0).abs().idxmin(1)
df['result'] = df2.lookup(df2.index, idx.fillna('v1'))

You can also index into the underlying NumPy array by getting integer indices using df.columns.get_indexer.

# idx = df[['A', 'B', 'C']].sub(df.target, axis=0).abs().idxmin(1)
idx = df.drop(['index', 'target'], 1).sub(df.target, axis=0).abs().idxmin(1)
# df['result'] = df.values[np.arange(len(df)), df.columns.get_indexer(idx)]
df['result'] = df.values[df.index, df.columns.get_indexer(idx)]

df
   index   A   B   C  target  result
0      1  11   8  10      12      11
1      2  17   6  17      13      17
2      3   5  16  12       8       5
3      4   9  17  13       6       9
4      5  10   9  15      12      10
like image 84
cs95 Avatar answered Apr 28 '23 13:04

cs95