0

I have a dataframe:

df = pd.DataFrame([1,2,3,4,5,6])

I have created a function which takes dataframe and % split as input and creates two new dataframes based on inputs

def splitdf(df,split=0.5):

    a = df.iloc[:int(len(df)]*split)]
    b = df.iloc[int((1-split)*len(df)):]

Now, when I run this function and call "a"

splitdf(df)
display(a)

I get the error: name 'a' is not defined

Ravan
  • 35
  • 7

1 Answers1

0

a and b are local to splitdf, so you have to return them.

def splitdf(df,split=0.5):
    a = df.iloc[:int(len(df)]*split)]
    b = df.iloc[int((1-split)*len(df)):]
    return a, b

Then when you call splitdf, assign the return values to some variable(s):

df_a, df_b = splitdf(df)

You are currently just defining a and b within splitdf, and then they stop existing when the function exits because they've gone out of scope.

mRotten
  • 447
  • 1
  • 4
  • 11