0

I have created a function as below to create a dataframe from a bigger dataframe

def prepare_data(cyl,typ):
global variable_name
global variable_name2
mask_bel1800 = (data_train_bel1800['Cycle'] == cyl) & (data_train_bel1800['Type'] == typ)
variable_name = ("DF{c}_{s}".format(c=cyl, s=typ))
variable_name = data_train_bel1800.loc[mask_bel1800, :]


mask = (data_train['Cycle'] == cyl) & (data_train['Type'] == typ)
variable_name2 = ("DF{c}_{s}_full".format(c=cyl, s=typ))
variable_name2 = data_train.loc[mask, :]

print('dataframe ' +'"DF'+str(cyl)+'_'+str(typ) +'"'+ " upto 1800 is ready")
print('dataframe ' +'"DF'+str(cyl)+'_'+str(typ)+'_full'+'"' " is ready")

When I put the statement print(variable_name) inside this function, the dataframe is printed. However, after i run this function & then I try to access the dataframe with df.head(), i get the error 'df is not defined'!!! what i am doing wrong?

Error i am getting is below enter image description here

If I 'print' the dataframe directly, it prints (pic below) enter image description here

I checked to make sure i have the right dataframe name by print just the variable "Variable name" and that is correct also![enter image description here]3

2
  • this question does not seem complete... see if you can put more samples so we recreate the issue. Commented Jun 26, 2019 at 9:47
  • the df created depends on "cyl" and "typ". Since "cyl" and "typ" change almost always, i wanted the new df that is created to have the name of these values. So, i am assigning the "variable name" variable would be "DF9_1" if i pass cyl as 9 & typ as 1 (these values are used to create mask for the new df). Then a df named "DF9_1" gets created. I am using the print statement bove only to print the name of the dataframe that has been created. However, when i try to access thes dataframe that is actually created, "DF9_1" for example, i get the error "NameError: name 'DF9_1' is not defined" Commented Jun 26, 2019 at 9:59

1 Answer 1

2

I think your function is missing the return statement so it returns None. You also need to assign the return value of a function to a variable to be able to use it later. For example:

def prepare_data(data, cyl, typ):
    mask = (data['Cyl'] == cyl) & (data['Typ'] == typ)
    prepared = data.loc[mask, :]
    print(f'Dataframe {cyl}_{typ}_full created.')
    return prepared

Now you will be able to call the function and print the result like this:

df = prepare_data(data_train, cyl, typ)
print(df)

The function uses data_train, cyl and typ as the input and returns prepared. That means that df outside the function is now what prepared was inside the function.

Sign up to request clarification or add additional context in comments.

4 Comments

Unfortunately, still not working."return" statement prints the dataframe as well. I just need the function to create the dataframe & let me knwo the names of the dataframe so that i can work with them later
return does not print the dataframe. Return says what the output of the function is. If you are using ipython or jupyter notebooks (it looks like you are), then running a cell like prepare_data(data_train, cyl, typ) will indeed print the dataframe. That is a feature of the notebooks. But running a cell like df = prepare_data(data_train, cyl, typ) will not print the dataframe. Instead, it will take the dataframe and give it a label df which you can use to refer to the dataframe.
Correct. I tried it again & df was indeed the dataframe. However, in my function, i am creating 2 dataframes. df will be the first dataframe. How do i pass the second dataframe to a variable like this?
If you simply want to create another dataframe using the same function with different arguments, you should just call the function again df2 = prepare_data(data_train, cyl2, typ2). A function can return pretty much anything. If you change the line to return (df1, df2), it will return a tuple of df1 and df2 (assuming that df1 and df2 exist within the scope of the function!). You can then even unpack the tuple like this: df1, df2 = prepare_data(data_train, cyl, typ).

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.