mardi 14 août 2018

Python Pandas: Read in External Dataset Into Dataframe Only If Conditions Are Met Using Function Call

Let's say I have an Excel file called "test.xlsx" on my local machine. I can read this data set in using traditional code.

df_test = pd.read_excel('test.xlsx') 

However, I want to conditionally read that data set in if a condition is met ... if another condition is met I want to read in a different dataset.

Below is the code I tried using a function:

def conditional_run(x):

    if x == 'fleet':
        eval('''df_test = pd.read_excel('test.xlsx')''')

    elif x != 'fleet':
        eval('''df_test2 = pd.read_excel('test_2.xlsx')''')

conditional_run('fleet') 

Below is the error I get:

File "<string>", line 1
    df_test = pd.read_excel('0Day Work Items Raw Data.xlsx')
            ^
SyntaxError: invalid syntax

Aucun commentaire:

Enregistrer un commentaire