Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.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-搜索2张excel工作表中的数据,然后提取数据标签_Python_Excel_Pandas - Fatal编程技术网

Python-搜索2张excel工作表中的数据,然后提取数据标签

Python-搜索2张excel工作表中的数据,然后提取数据标签,python,excel,pandas,Python,Excel,Pandas,我对编程非常陌生,不得不学习如何使我的博士项目的一部分真正可行,但在获得python代码从网站提取数据并将其写入excel文件后,我对下一部分有点茫然 我有我的excel文件和提供的辅助文件。我试图在两个文件之间搜索(两个文件都有“地址”,一旦匹配,从提供的文件中提取一个标签(在类别下),并将其输入myfile。或者,如果这更容易,只需将结果写入一个全新的excel文件 “我的文件” #|地址| 新罕布什尔州法尔科克阿伯茨福德街1 | 21号FK2 7NH| 2 |博内斯街警察局局长EH51 9

我对编程非常陌生,不得不学习如何使我的博士项目的一部分真正可行,但在获得python代码从网站提取数据并将其写入excel文件后,我对下一部分有点茫然

我有我的excel文件和提供的辅助文件。我试图在两个文件之间搜索(两个文件都有“地址”,一旦匹配,从提供的文件中提取一个标签(在类别下),并将其输入myfile。或者,如果这更容易,只需将结果写入一个全新的excel文件

“我的文件”

#|地址|
新罕布什尔州法尔科克阿伯茨福德街1 | 21号FK2 7NH|
2 |博内斯街警察局局长EH51 9AF|
3 | 4 Bo'ness河景台EH51 9ED |

结果文件

#|地址|类别
新罕布什尔州法尔科克市阿伯茨福德街1号| 21 FK2 7NH | A
2 |警察局局长街Bo'ness EH51 9AF | B
3 | 4 Bo'ness河景台EH51 9ED | A

我面临的问题是,这两个文件的“地址”数据没有任何特定顺序,因此我如何从myfile中获取地址,在提供的文件中搜索地址,提取“类别”,然后将地址/类别合并到结果文件中(甚至只是将“类别”添加到myfile中)

如果这一点不清楚,我也非常抱歉,我尽了最大努力正确地表达了这一点,但感谢您提供的任何建议,甚至是我可以研究的扩展,以帮助解决这一问题。

只需运行一个与地址的精确值匹配的左连接:


我不完全确定您是否正在尝试将两个文件合并为一个文件并检查地址副本,或者您是否正在尝试检查myfiles中的地址是否包含在第二个文件中,因此我尝试在下面提供这两个文件

如果要合并和比较两个文件:

import pandas as pd

# Reads myfile Excel file to new data frame
df1 = pd.read_excel("C:/folder/myfile.xlsx")
# Reads second file (a .csv in this example) to new data frame
df2 = pd.read_csv("C:/folder/secondfile.csv")

# Creates a third data frame containing df1 and df2
df3 = df1.append(df2)
# Checks for duplicates and creates a new column labeling which are duplicates
df3["duplicate"] = df3.duplicated(subset="Address", keep=False)
# Removes all but the first duplicate in the Address column
df3 = df3.drop_duplicates(subset="Address")

# Writes df3 to Excel file
df3.to_excel("C:/folder/outcomefile.xlsx", index=False)
import pandas as pd

# Reads myfile Excel file to new data frame
df1 = pd.read_excel("C:/folder/myfile.xlsx")
# Reads second file (a .csv in this example) to new data frame
df2 = pd.read_csv("C:/folder/secondfile.csv")

# Checks if addresses in myfile (df1) are duplicated in second file (df2)
# Then adds duplicate column to myfile
df1["duplicate"] = df1["Address"].isin(df2["Address"])

# Writes edited myfile data frame to new Excel file
df1.to_excel("C:/folder/outcomefile.xlsx", index=False)
注意:仅当两个文件具有相同的列时,此操作才有效

如果要检查myfile中的地址在第二个文件中是否重复,请在myfile中添加重复列并打印到新的Excel文件:

import pandas as pd

# Reads myfile Excel file to new data frame
df1 = pd.read_excel("C:/folder/myfile.xlsx")
# Reads second file (a .csv in this example) to new data frame
df2 = pd.read_csv("C:/folder/secondfile.csv")

# Creates a third data frame containing df1 and df2
df3 = df1.append(df2)
# Checks for duplicates and creates a new column labeling which are duplicates
df3["duplicate"] = df3.duplicated(subset="Address", keep=False)
# Removes all but the first duplicate in the Address column
df3 = df3.drop_duplicates(subset="Address")

# Writes df3 to Excel file
df3.to_excel("C:/folder/outcomefile.xlsx", index=False)
import pandas as pd

# Reads myfile Excel file to new data frame
df1 = pd.read_excel("C:/folder/myfile.xlsx")
# Reads second file (a .csv in this example) to new data frame
df2 = pd.read_csv("C:/folder/secondfile.csv")

# Checks if addresses in myfile (df1) are duplicated in second file (df2)
# Then adds duplicate column to myfile
df1["duplicate"] = df1["Address"].isin(df2["Address"])

# Writes edited myfile data frame to new Excel file
df1.to_excel("C:/folder/outcomefile.xlsx", index=False)
注意:两者仅匹配准确的地址


希望这对您有所帮助,如果您需要任何不同的信息,请告诉我!

在我看来,使用Excel以外的文件应该更容易,这样您就可以使用
pd.read\u Excel()
将它们放入
数据框中,然后在地址上合并。感谢这两个选项…不幸的是,我在提供的文件中发现了一个错误,因为它是一个不完整的引用。我现在必须使用基于web的搜索从我的文件地址,然后希望以这种方式提取结果。我对此充满希望这将只是两个excel文件使用!但再次感谢这两个代码,我相信,一旦我有了新的代码/搜索程序,它们将派上用场