exec function doesn't work inside a function

20 Views Asked by At

I'm using the exec function, and I would like to understand why it doesn't work inside a function. In the following example I expect the ds_test dataframe to be created, but it does not

import pandas as pd
def test_func():
    string = "ds_test = pd.DataFrame({ 'col1': ['A', 'A', 'B', np.nan, 'D', 'C'], 'col2': [2, 1, 9, 9, 7, 4], 'col3': [0, 1, 9, 4, 2, 3] })"
    print(string)
    exec(string)
test_func()
0

There are 0 best solutions below