简体   繁体   English

在另一列的列表中查找与 Pandas 数据框列最近的元素

[英]Find closest element of a pandas dataframe column in another column's list

I have the following dataframe:我有以下数据框:

A = [3,38,124]
B = [[0,0,1,7,34,76,4,15,28,8,7,8,200,108,7],[0,0,1,7,34], 
    [4,109,71,257,3,3,7,1,0,0,7,8,100,148,54,3,134,90,23,43,17]]

df = pd.DataFrame({'A':A,
   'B':B})
df 

The column B has lists as elements. B 列将列表作为元素。 I want to create a new column with the closest element to column A contained in the corresponding list of B.我想创建一个新列,其中包含与 B 的相应列表中包含的列 A 最接近的元素。

Desired output :所需的输出:

A = [3,38,124]
B = [[0,0,1,7,34,76,4,15,28,8,7,8,200,108,7],[0,0,1,7,34], 
[4,109,71,257,3,3,7,1,0,0,7,8,100,148,54,3,134,90,23,43,17]]
Desired_output=[4,34,134]
df_out = pd.DataFrame({'A':A,
   'B':B,
              'Desired_output':Desired_output})
df_out=df_out [['A','B','Desired_output']]
df_out 

Try doing that before you put it into a DataFrame , like this:在将其放入DataFrame之前尝试这样做,如下所示:

C = [B[i][np.argmin(np.abs(np.array(B[i]) - A[i]))] for i in range(len(A))]
df = pd.DataFrame({'A':A,
   'B':B, 'Closest':C})

The output is:输出是:

     A                                                B    Closest
0    3  [0, 0, 1, 7, 34, 76, 4, 15, 28, 8, 7, 8, 200, ...    4
1   38                                   [0, 0, 1, 7, 34]   34
2  124  [4, 109, 71, 257, 3, 3, 7, 1, 0, 0, 7, 8, 100,...  134

To complete the previous answer, if you want to do this after you put your data in a DataFrame , use the DataFrame.apply function as follows:要完成上一个答案,如果您想在将数据放入DataFrame之后执行此DataFrame ,请使用DataFrame.apply函数,如下所示:

import pandas as pd
import numpy as np

A = [3, 38, 124]
B = [[0, 0, 1, 7, 34, 76, 4, 15, 28, 8, 7, 8, 200, 108, 7],
     [0, 0, 1, 7, 34],
     [4, 109, 71, 257, 3, 3, 7, 1, 0, 0, 7, 8, 100, 148, 54, 3, 134, 90, 23, 43, 17]]
df = pd.DataFrame({'A': A, 'B': B})

def find_nearest(row):
    return row["B"][np.argmin([abs(candidate-row["A"]) for candidate in row["B"]])]

df["desired_output"] = df.apply(find_nearest, axis=1)

print(df)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找到最接近 pandas dataframe 的列 - Find closest column to pandas dataframe avg Python pandas 在另一列的元素列表中查找一列的元素 - Python pandas find element of one column in list of elements of another column 如何为一列中的每个元素找到另一列中最接近的元素? - How to find the closest element in another column for each element in a column? 查找 pandas 列中的值是否在另一列的列表中 - find if the value in pandas column is in another column's list Pandas Dataframe:找到坐标点与另一列坐标点最近的列 - Pandas Dataframe: Find the column with the closest coordinate point to another columns coordinate point 在一个列表中查找项目,但不在熊猫数据框列中的另一个列表中查找项目 - Find items in one list but not in another in a pandas dataframe column Pandas 将数据帧列中的列表与另一个数据帧合并 - Pandas merge a list in a dataframe column with another dataframe python pandas:检查数据帧的列值是否在另一个数据帧的列中,然后计数并列出它 - python pandas: Check if dataframe's column value is in another dataframe's column, then count and list it 替换 pandas dataframe 列中的元素(如果存在于另一个 dataframe 列中) - Replace an element in a pandas dataframe column if present in another dataframe column 查找包含整数列的 pandas dataframe 与包含整数列表列的 dataframe 匹配的所有实例 - Find all instances where a pandas dataframe containing a column of int's matches a dataframe with a column of a list of ints
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM