Search code examples
pythonpandasfunctiondataframereturn

Use a pandas dataframe created by function 1 in function 2?


I have a function 1 which creates a pandas dataframe by importing data from excel. Now I need to work with that data in a different function 2, but I dont manage to get the dataframe from function 1 back to the main code aswell as in function 2.

I cant publish the original code, but here is a simplified example of what it looks like

I got error: name 'parameter' is not defined

def function1()
    parameter = pd.read_excel("IDA_IDs.xlsx")
    return parameter

def function2(parameter)
    print(parameter)

function1()
function2(parameter)

Solution

  • Another option is that you can define it as a global as well. If you want to act on the global data in your function/method, you should not pass it as a parameter.

    def function1()
        global data
        data = pd.read_excel("IDA_IDs.xlsx")
    
    def function2()
        print(data)
    
    function1()
    function2()