Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas- for each row in Data frame check if value exist in excel file

I have the Data frame DF1:

Value Path Result
val1 C:\file1.xlsx True
val2 C:\file2.xlsx False

I need to check for each row if value is in the excel file at the specific row path and update Result accordingly.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your data table

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading