从多个列中查找最接近的值,然后添加到Python中的新列

时间:2018-12-29 13:05:47

标签: python pandas 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)

我想在A,B和C列中找到最接近列目标的值,并将这些值放入列结果中。据我所知,我需要使用abs()和argmin()函数。 这是我期望的输出:

     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

这是解决方案,它链接了我从stackoverflow中发现的内容,这可能会有所帮助:

(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

2 个答案:

答案 0 :(得分:5)

从其他列中减去“目标”,使用idxmin获得具有最小差异的列,然后是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

处理字符串列和NaN的常规解决方案(以及您将目标中的NaN值替换为“ 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'))

您还可以通过使用df.columns.get_indexer获取整数索引来索引基础的NumPy数组。

# 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

答案 1 :(得分:5)

您可以将NumPy 位置整数索引argmin一起使用:

col_lst = list('ABC')
col_indices = df[col_lst].sub(df['target'], axis=0).abs().values.argmin(1)
df['result'] = df[col_lst].values[np.arange(len(df.index)), col_indices]

或者您可以lookup idxmin 列标签

col_labels = df[list('ABC')].sub(df['target'], axis=0).abs().idxmin(1)
df['result'] = df.lookup(df.index, col_labels)

print(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

原理是相同的,尽管对于较大的数据帧,您可能会发现NumPy更有效:

# Python 3.7, NumPy 1.14.3, Pandas 0.23.0

def np_lookup(df):
    col_indices = df[list('ABC')].sub(df['target'], axis=0).abs().values.argmin(1)
    df['result'] = df[list('ABC')].values[np.arange(len(df.index)), col_indices]
    return df

def pd_lookup(df):
    col_labels = df[list('ABC')].sub(df['target'], axis=0).abs().idxmin(1)
    df['result'] = df.lookup(df.index, col_labels)
    return df

df = pd.concat([df]*10**4, ignore_index=True)

assert df.pipe(pd_lookup).equals(df.pipe(np_lookup))

%timeit df.pipe(np_lookup)  # 7.09 ms
%timeit df.pipe(pd_lookup)  # 67.8 ms