简体   繁体   English

如何比较两列并从第三列返回值 Pandas dataframe

[英]How to compare two columns and return value from a third column in Pandas dataframe

sample data样本数据

sample = ({'name':['Delinquency Rate','Cumulative Probability'],
           'value':['Dlnqy','Prbblty'],
           'new_name':['Dlnqncy Rt','Cmltv Prbblty']})

test = pd.DataFrame(sample)

test

new_name is created by removing all the vowels from 'name'. new_name 是通过从“name”中删除所有元音创建的。 I want to compare first 3 characters of 'value' and first 3 character of 'each of the word' in 'new_name' and if they match I want to return the value from the name column.我想比较“新名称”中“值”的前 3 个字符和“每个单词”的前 3 个字符,如果它们匹配,我想从名称列中返回值。 For example 'Dln' in value exists in 'Dlnqncy' of new_name, so we will return 'Delinquency' from name.例如'Dln' in value 存在于new_name 的'Dlnqncy' 中,所以我们将从name 中返回'Delinquency'。 The logic should work like逻辑应该像

'if value[:3] in new_name[:3] then return name'

Following is what I have so far, which works fine if I have only two columns.以下是我到目前为止所拥有的,如果我只有两列,它就可以正常工作。 But it doesn't work if I want to compare value and new_name and retun name.但是如果我想比较 value 和 new_name 并重新调整名称,它就不起作用了。

def get_matches(name, value, new_name, default=''):
    return next( (word for word in new_name.split() if str(value)[:3] in word[:3]),default)


test['match'] = test[['name', 'value', 'new_name']].apply(lambda row: get_matches(*row, default=' '), axis=1)

In the following table the column 'match' should have 'Delinquency' and 'Probability' (as it is there in the 'name' column)在下表中,“匹配”列应该有“拖欠”和“概率”(因为它在“名称”列中)

在此处输入图像描述

Here you go:给你go:

(test
 .assign(match=lambda x: np.where((x.new_name.str[:3] == x.value.str[:3]), x.name, x.value))
)

暂无
暂无

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

相关问题 比较 pandas DataFrame 中的两个日期列以验证第三列 - Compare two date columns in pandas DataFrame to validate third column Pandas 比较两个数据框中的两列,如果有匹配项,则从第三列获取值转换为周数 - Pandas compare two columns from two dataframes if there is a match get value from third column convert to week number 如何比较pandas中的两列来制作第三列? - how to compare two columns in pandas to make a third column ? 如何比较两个 dataframe 列并将第三列值提取为 python 中的 output - How to compare two dataframe columns and extract third column value as output in python 如何比较 DataFrame 中的两列并根据该比较更改第三列的值? - How to compare two columns in DataFrame and change value of third column based on that comparison? 如何比较 Pandas Dataframe 中的两列以查找匹配百分比并根据该逻辑返回一个值? - How do I compare two columns in Pandas Dataframe to find the match percentages and return a value based on that logic? Python / Pandas - 如何按两列分组,并计算两行之间第三列的值 - Python/Pandas - How to group by two columns and count rows with value from third column between two numbers 如何比较两个 pandas dataframe 行并返回值? - How to compare two pandas dataframe rows and return the value? Dataframe - 对于每一行,比较两列的值,匹配时获取第三列的值 - Dataframe - for each row, compare values of two columns, get value of third column on match Pandas 比较不同数据帧的两列,如果匹配,则复制第三列的值 - Pandas compare two columns of different dataframes and copy value of a third column if there is a match
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM