-2

I'd like to use a dataframe in function_Bwhich is produced by function_A

def function_A():
   df = pandas.DataFrame(data,columns=['A'])
   return df

def function_B():
   df1 = function_A()

if __name__ == '__main__':
   function_A()
   function_B()

However, df1 and df got difference.

df is like

    A
0   aaa

1   bbb

And df1 is an EMPTY dataframe. Anyone knows the reason of it?

EDIT

I want to concatenate df1 with another dataframe in funtion_B.

def function_B():
   df1 = function_A()
   df2 = pandas.DataFrame(data2,columns=['A'])
   pandas.concat([df1,df2])

Is there any other solutions except return df1 which was referred by most of the answers.

Jason Shu
  • 139
  • 1
  • 2
  • 11
  • 1
    You need to `return df1` in `function_B()` – Mayank Porwal Oct 18 '18 at 09:23
  • Every time you call `function_B()` it defines `df1` by calling `function_A()`, i.e. it will create a new dataframe for each call of function B. You can use an existing dataframe by giving it as a parameter. – Thijs van Ede Oct 18 '18 at 09:23
  • Your edit seems unclear. Can you provide what exact output you expect from the functions? Pinging @jpp because there might be another specific dupe target due to the recent edit. – shad0w_wa1k3r Oct 18 '18 at 15:37
  • 1
    @shad0w_wa1k3r, Seen the edit, still very unclear. E.g. Why *not* return values. The question needs to explain the *problem* with using `return`. – jpp Oct 18 '18 at 15:41

2 Answers2

0

It is not empty. Just fix function_B:

def function_B():
    df1 = function_A()
    return df1

And of course id(function_A()) and id(function_B()) are not equal (e.g. dataframes are not the same object because you create a new Dataframe each time).

Matphy
  • 1,086
  • 13
  • 21
  • I want to concatenate df1 with another dataframe in funtion_B. Is there any other solutions except return df1. List in my edit. – Jason Shu Oct 18 '18 at 09:54
0

Maybe you just need to add a return to your function_b:

def function_B():
   df1 = function_A()
   return df1
Tina Iris
  • 551
  • 3
  • 8
  • I want to concatenate df1 with another dataframe in funtion_B. Is there any other solutions except return df1. See my edit – Jason Shu Oct 18 '18 at 09:53