Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用数据库中dataframe1中一列的值查找dataframe2中特定列的值_Python_Pandas_Dataframe_String Comparison - Fatal编程技术网

Python 使用数据库中dataframe1中一列的值查找dataframe2中特定列的值

Python 使用数据库中dataframe1中一列的值查找dataframe2中特定列的值,python,pandas,dataframe,string-comparison,Python,Pandas,Dataframe,String Comparison,我在网上搜索了一下。我没有找到我要找的确切病例。 我需要帮助。我有两个数据帧,其中一列包含类似的项 >>> df1 ID Item 0 0667170D Apple 1 0644304D Orange 2 0655323D Pineapple 3 06284A3D Banana >>> df2 ID Item 0 TY671756 Carrot 1 JG444

我在网上搜索了一下。我没有找到我要找的确切病例。 我需要帮助。我有两个数据帧,其中一列包含类似的项

>>> df1
         ID       Item
0  0667170D      Apple
1  0644304D     Orange
2  0655323D  Pineapple
3  06284A3D     Banana
>>> df2
          ID    Item
0   TY671756  Carrot
1   JG44454D  Banana
2   07753DDD  Orange
3   0628456D   Apple
我有一个forloop,它将比较两个数据帧之间的Item列,并得到最接近的数据帧。例:我从'df2'中选取苹果,并将其与df1中的'Item'列进行比较。我找到apple,并将其作为匹配项更新到df2中的一个新列。现在我想在“df1”中找到匹配项的苹果的“ID”,在本例中是苹果。我想将df1中苹果的“ID”更新为df2中的一个新列

我也能在同样的forloop中这样做吗?因此,我得到一个更新的df2,其中包含在df1中找到的匹配项及其ID号

list1 = df2['Item']
list2 = df1['Item']

for i in list1:
   df2['Item'] = [difflib.get_close_matches(i, list2)]
项上的两个dfs

df3=df1.merge(df2,on="Item")
这将为您提供两个数据帧中的匹配项及其ID

   ID_x    Item      ID_y
0  0667170D   Apple  0628456D
1  0644304D  Orange  07753DDD
2  06284A3D  Banana  JG44454D
如果还希望保留不匹配的项目:

df1.merge(df2,on="Item",how="outer")

       ID_x       Item      ID_y
0  0667170D      Apple  0628456D
1  0644304D     Orange  07753DDD
2  0655323D  Pineapple       NaN
3  06284A3D     Banana  JG44454D
4       NaN     Carrot  TY671756

如果需要,可以重命名列。

我认为需要按字典查找-输出是列表,因为一个或多个值是匹配的:

list1 = df2['Item']
list2 = df1['Item']

d = df1.set_index('Item')['ID']
df2['new'] = [[d[x] for x in difflib.get_close_matches(i, list2)] for i in list1]
print (df2)
         ID    Item         new
0  TY671756  Carrot          []
1  JG44454D  Banana  [06284A3D]
2  07753DDD  Orange  [0644304D]
3  0628456D   Apple  [0667170D]
编辑:对于输出,两列都使用
循环
解决方案:

list1 = df2['Item']
list2 = df1['Item']
d = df1.set_index('Item')['ID']

id2, item2 = [], []
for i in list1:
     out =  difflib.get_close_matches(i, list2)
     id2.append([d[x] for x in out])
     item2.append(out)

df2['id2new'] = id2    
df2['item2new'] = item2
print (df2)
         ID    Item      id2new  item2new
0  TY671756  Carrot          []        []
1  JG44454D  Banana  [06284A3D]  [Banana]
2  07753DDD  Orange  [0644304D]  [Orange]
3  0628456D   Apple  [0667170D]   [Apple]

如果你想使用下面的代码来执行这个循环,我想可以使用下面的代码。否则,您可以使用@Sruthi V的答案

newColumn = []
for value in df2['Item'].values:
    if (len(df1[df1['Item']==value].values) > 0):
        newColumn.append(df1[df1['Item']==value].iloc[0,0])
    else:
        newColumn.append(np.NaN)

df2['NewColumn'] = newColumn


>>> df2

         ID    Item NewColumn
0  TY671756  Carrot       NaN
1  JG44454D  Banana  06284A3D
2  07753DDD  Orange  0644304D
3  0628456D   Apple  0667170D

将要回答,但这似乎更好、更简单,解决方案使用示例数据,但没有
get\u close\u matches
,因此这是错误的…@Sruthi V。感谢您的回答,但如果使用合并或联接选项,将忽略循环的字符串匹配。我之前确实研究过这些,但不适合我的需要。谢谢。这确实有效。但是在最初的问题中遗漏了一点,我用了一个forloop来匹配Item列。所以我需要两个新列,一个用于匹配项,另一个用于匹配ID。嗯,我发现了如何获取匹配项,但我想知道是否可以将ID放入同一for循环中的另一个新列中。希望我的问题是清楚的。对不起,刚才有点混乱,谢谢。这确实能准确地工作。但是在最初的问题中遗漏了一点,我用了一个forloop来匹配Item列。所以我需要两个新列,一个用于匹配项,另一个用于匹配ID。嗯,我发现了如何获取匹配项,但我想知道是否可以将ID放入同一for循环中的另一个新列中。希望我的问题是清楚的。很抱歉之前出现了任何混乱。您可以在python3控制台上运行此操作吗。您将看到我正在寻找的输出类型。>>>df2=pd.DataFrame({'ID':pd.Category(['0TY671756','JG44454D','07753DD','0628456D']),'Item':pd.Category(['Carrot','Banana','Orange','Apple']),'MatchedID':pd.Category(['['],'06284A3D]','0644304D]','0667170D]],'Matched Item:'pd.Category(['['],'香蕉],'Orange]','Apple]]))