如何取2个参数并应用函数

2024-06-11 17:15:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据帧需要在uid\u has\u new和uid\u hash上创建模糊比率/hamming距离并创建一个新表

 uid    uid_hash  uid_hash_new
1   123 ABC28071  ABC28079
4   121 ABC28071  ABC28089

伪代码

import pandas as pd
def ratio(x,y):
    return
df['ratio'] = df['uid_hash_new','uid_hash'].apply(ratio)

Tags: 数据代码import距离dfnewuidhash
2条回答

你可以做:

def ratio(a):
    return a[0]+a[1]

df['ratio'] = df['uid_hash_new','uid_hash'].apply(ratio, axis=1)

所以a[0]将是uid_hash_new(x)和a[1]uid_hash(y)

^{}与lambda函数一起使用:

df['ratio'] = df.apply(lambda x: ratio(x['uid_hash_new'], x['uid_hash']), axis=1)

相关问题 更多 >