地址匹配两列python
假设我在一个数据帧中有两列: 第1列:地址匹配两列python,python,regex,python-3.x,string-matching,Python,Regex,Python 3.x,String Matching,假设我在一个数据帧中有两列: 第1列: for (item, Value),(item1, Value1) in zip(df1['Column1'].iteritems(),df2['Column2'].iteritems()): if str(Value).strip() in str(Value1).strip(): found.append(1) 第1行:堆栈溢出 第2行:Python 第2列: for (item, Value),(item1, Value1)
for (item, Value),(item1, Value1) in zip(df1['Column1'].iteritems(),df2['Column2'].iteritems()):
if str(Value).strip() in str(Value1).strip():
found.append(1)
第1行:堆栈溢出
第2行:Python
第2列:
for (item, Value),(item1, Value1) in zip(df1['Column1'].iteritems(),df2['Column2'].iteritems()):
if str(Value).strip() in str(Value1).strip():
found.append(1)
第1行:['Stack','Stack Overflow']
第2行:[“Python编程”、“Python蛇”]
我想按行进行精确匹配(可选),并相应地返回一个标志
输出:
for (item, Value),(item1, Value1) in zip(df1['Column1'].iteritems(),df2['Column2'].iteritems()):
if str(Value).strip() in str(Value1).strip():
found.append(1)
[0]匹配
[1] 不匹配
已尝试:
我曾在循环中尝试过“in”函数,但这会使部分匹配也成为“match”
代码:
for (item, Value),(item1, Value1) in zip(df1['Column1'].iteritems(),df2['Column2'].iteritems()):
if str(Value).strip() in str(Value1).strip():
found.append(1)
我认为你需要:
def isMatch(row):
for i in row['b']:
if i == row['a']:
return 'Match'
return 'Not Match'
df['c'] = df.apply(lambda x: isMatch(x), axis=1)
print(df)
好的,我会尝试回答这个问题,如果其他人有类似的问题。基本上,您需要检查
col1
值是否位于col2
(列表)中。您可以轻松地使用isin
。
在函数中应用numpy,可以创建一个标志
这是一个模型
df = pd.DataFrame({
'col1': ['Stack Overflow', 'Python'],
'col2': [ ['Stack', 'Stack Overflow'], ['Python Programming', 'Python Snake']]})
df['Flag'] =df.apply(lambda x: x['col1'] in x['col2'], axis=1)
df
结果如下:
col1 col2 Flag
0 Stack Overflow [Stack, Stack Overflow] True
1 Python [Python Programming, Python Snake] False
让我知道它是否有效。您能与我们分享您过去尝试的代码吗?请将工作代码.for(item,Value),(item1,Value1)放在zip中(df1['Column1'].iteritems(),df2['Column2'].iteritems()):如果str(Value).strip()放在str(Value1.strip():found.append(1)@deepankargargarg编辑您的问题并添加代码…不要在评论中发布Hanks,我们马上就到了。如果您可以创建一个示例数据帧df来突出显示问题,然后将您尝试过的代码放入其中,这将非常有帮助。不,这对我不起作用。您是否尝试了我提供的相同数据集?相同的格式?它将所有“不匹配”返回给我。我的一些记录确实匹配。我认为我的第二列不是一个包含字符串的列表,这就是它可能无法匹配的原因。“['rampuri,kalkaji','tughlakabad extension,tughlakabad','govindpuri rd,govindpuri','govindpuri','giri nagar,kalkaji']”上面是我的第二列的一行。