Home > Blockchain >  Creating a DataFrame in one function and using it in another function
Creating a DataFrame in one function and using it in another function

Time:09-04

I am currently loading in a CSV file and displaying that CSV file to a LabelFrame

On a different LabelFrame I have a "mean" button to calculate the mean of the data. However I am getting this error

NameError: name 'df' is not defined

I have a DataFrame that I created in a function called load_data() that outputs the data to the LabelFrame. My next objective is to press the "mean" button and have it calculate the mean. I figured maybe it was not working because that variable isn't global. However, I did this:

 `def compute_mean():
       global df
       calcMean = df.mean()`

df is being used in my load_data() function as follows:

 `df = pd.read_csv(filename)`

CodePudding user response:

Try avoiding globals. Pass the dataframe to your function instead.

def compute_mean(df):
    return df.mean()

df = pd.read_csv(filename)

mean = compute_mean(df)

Of course, in this case, the function is just a wrapper to df.mean(), so it can be avoided.

df = pd.read_csv(filename)
mean = df.mean()

If you have a load_data() function, then be sure it returns the dataframe.

def load_data(filename):
    return pd.read_csv(filename)

df = load_data(filename)
mean = df.mean()

CodePudding user response:

Did you return df in you load_data function? Something like this:

def load_data(filename):
    df = pd.read_csv(filename)
    return df

Then you use load_data function inside compute_mean, here df is assigned to x:

def compute_mean(somefile):
    x = load_data(somefile)
    calcMean = x.mean()
    return calcMean
  • Related