0

I have an initial dataframe called source_data.

I have a separate text file which allows a user to place some python code in for some filter criteria. I know this is seen as bad practice, but my company has assured me that it's safe to do. I've noted my concerns but I have the green light.

So, this CSV has this string in a cell: source_data.loc[source_data['CONFIG_ID'] == 3]

This is read in as a list. As an example, here's two prints:

print('Number of where clause conditions found: ' + str(len(segmentFilterCriteria)))
print('Where clause criteria: ' + str(segmentFilterCriteria))

Console shows:

Number of where clause conditions found: 1
Where clause criteria: ["source_data.loc[source_data['CONFIG_ID'] == 3]"]

Now, when I run this (i.e. using the text string and the exec function), my base_data dataframe appears as NoneType:

print(segmentFilterCriteria[0])
base_data = exec(segmentFilterCriteria[0])
print(base_data.columns)

Console shows:

source_data.loc[source_data['CONFIG_ID'] == 3]
Traceback (most recent call last):

  File "<ipython-input-95-17bc7d85852e>", line 3, in <module>
    print(base_data.columns)

AttributeError: 'NoneType' object has no attribute 'columns'

However, when I just type the code out, like this, it works fine:

base_data = source_data.loc[source_data['CONFIG_ID'] == 3]
print(base_data.columns)

Console shows:

Index(['CONFIG_ID', 'ACCOUNT_DEAL_ID', 'FACILITY_ID',
       'M12_PD_INITIAL_RECOG_VAL',
...

Why does my string executed inside an exec function not work in this case?

1 Answer 1

3

exec() function doesn't return any result. It always returns None. You should use it like this:

print(segmentFilterCriteria[0])
exec('base_data=' + segmentFilterCriteria[0])
print(base_data.columns)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.