python: having trouble returning a pandas data frame from a user defined function (probably user error)

Viewed 11085

I have a function that creates a DataFrame. Within the function i can have it printed. But I am doing something wrong in the return process, because I can't seem to call the DataFrame after running the function. Below is my dummy code and the attached error.

import pandas as pd
def testfunction(new_df_to_output):
    new_df_to_output = pd.DataFrame()
    S1 = pd.Series([33,66], index=['a', 'b'])
    S2 = pd.Series([22,44], index=['a', 'b'])
    S3 = pd.Series([11,55], index=['a', 'b'])

    new_df_to_output = new_df_to_output.append([S1, S2, S3], ignore_index=True)
    print new_df_to_output
    print type(new_df_to_output)
    print dir()
    return new_df_to_output

testfunction('Desired_DF_name')

print dir()
print Desired_DF_name

The DataFrame prints properly within the function. The directory shows that the DataFrame is not returned after the function. Trying to print that dataframe returns returns the following error

Traceback (most recent call last): File "functiontest.py", line 21, in print Desired_DF_name NameError: name 'Desired_DF_name' is not defined

I am sure it is a simple mistake but I can't find the solution after searching Stackoverflow and python tutorials. Any guidance is greatly appreciated.

2 Answers
Related