Pandas-对于数据框中的每一行,检查excel文件中是否存在值



我有数据帧DF1:

C:\file2.xlsx
路径 结果
val1 C:\file1.xlsx
val2错误

您的数据表

import pandas as pd
import numpy as np
def create_dataframe():
data = {'Value': [1,2,3,4,5],
'Path': ['C:file1.xlsx','C:file2.xlsx','C:file3.xlsx','C:file4.xlsx','C:file5.xlsx'],
'Result': [True, False, True, False, True]}
df = pd.DataFrame(data)
return df

#checking if the value exists in the excel file
#if it does, return True, else return False
def check_value(df):
for index, row in df.iterrows():
try:
df.loc[index, 'Result'] = pd.read_excel(row['Path'], header=None).isin([row['Value']]).any()
except:
df.loc[index, 'Result'] = False
return df

如果Value位于名为Value:的列中,则会检查每个excel文件

进口熊猫作为pd

data = {'Value': ['val1','val2'],
'Path': ['/home/bera/Documents/test1.xlsx','/home/bera/Documents/test2.xlsx']}
df = pd.DataFrame(data)

yesno =[]
for i in df.index:
tempdf = pd.read_excel(df.iloc[i].Path)
yesno.append(df.iloc[i].Value in set(tempdf.Value))
df['Result'] = yesno

最新更新