简体   繁体   English

如何并排匹配2个以上

[英]How to match more than 2 side by side

I'm currently using python and having trouble finding a code.我目前正在使用 python,但找不到代码。 I have 2 data frames that I would like to match more than 2 numbers that match side by side.我有 2 个数据框,我想匹配 2 个以上并排匹配的数字。 For example, I would like to lookup the numbers in df1 and find more than 2 numbers match side by side in df2.例如,我想查找 df1 中的数字,并在 df2 中找到超过 2 个并排匹配的数字。

import pandas as pd 
cols = ['Num1','Num2','Num3','Num4','Num5','Num6']
df1 = pd.DataFrame([[2,4,6,8,9,10]], columns=cols)

df2 = pd.DataFrame([[1,1,2,4,5,6,8],
           [2,5,6,20,22,23,34],
           [3,8,12,13,34,45,46],
           [4,9,10,14,29,32,33],
           [5,1,22,13,23,33,35],
           [6,1,6,7,8,9,10],
           [7,0,2,3,5,6,8]], 
           columns = ['Id','Num1','Num2','Num3','Num4','Num5','Num6'])

I would like my results to be something like this.我希望我的结果是这样的。

results = pd.DataFrame([[6,1,6,7,8,9,10]],             
       columns = ['Id', 'Num1','Num2','Num3','Num4','Num5','Num6']) 

You can see Id 6 or index 6 has 8,9,10.您可以看到 Id 6 或索引 6 有 8、9、10。 More than 2 number match side by side.超过 2 个数字并排匹配。

i use this and it working !我用这个,它工作!

list1=df2[['Num1','Num2','Num3','Num4','Num5','Num6']].values.tolist()
list2=df1[['Num1','Num2','Num3','Num4','Num5','Num6']].values.tolist()
l=[]
for i in range(6):
    if list1[i][i]==list2[0][i]:
        l.append(i)
resultt=pd.DataFrame()
resultt=resultt.append(df2.loc[int(l[0])],ignore_index=True) 

out:出去:

    Id  Num1  Num2  Num3  Num4  Num5  Num6
0  6.0   1.0   6.0   7.0   8.0   9.0  10.0

i will working on it aleter to add condition of >2 it working because one row how has match the condition, but if is more than one it doasn't working我会更努力地添加 >2 的条件,因为一行如何匹配条件,但如果不止一个,它就不起作用

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM