应用函数结合lambda函数会比较两个列表中的每个元素。zip函数将按行对value1
和value2
列表中对应的元素进行迭代,并检查它们是否相等。
以下是Python代码实现:
import pandas as pd
# 定义数据
data = {'attribute': ['Address', 'Count', 'Color'],
'value1': [['a', 'b', 'c'], ['1', 2, 3], ['bl', 'cr', 'r']],
'value2': [['a', 'b', 'c'], ['1', '2', '3'], ['bl', 'rd', 'r']]}
# 创建DataFrame
df = pd.DataFrame(data)
# 应用lambda函数计算匹配情况并添加新列'match'
df['match'] = df.apply(lambda row: [x == y for x, y in zip(row['value1'], row['value2'])], axis=1)
# 输出结果
print(df)
运行上述代码后,你将得到如下所示的数据框:
attribute value1 value2 match
0 Address [a, b, c] [a, b, c] [True, True, True]
1 Count [1, 2, 3] [1, 2, 3] [True, False, False]
2 Color [bl, cr, r] [bl, rd, r] [True, False, True]