17

I want to return two data frames from a function, like this:

def test():
    df1 = pd.DataFrame([1,2,3], ['a','b','c'])
    df2 = pd.DataFrame([4,5,6], ['d','e','f'])
    return df1
    return df2
test()

But the function only returns one data frame df1. How to return both in pretty data frame format, not in cmd black background format?

When I tried to return both using

return df1, df2

in the Jupyter Notebook, the output returns the data frames in a black background cmd-like format, and not in proper data frame format.

2
  • 2
    I think you should just have one return statement. return df1, df2 Commented Sep 29, 2018 at 12:42
  • @TrippCannella That is what I don't want. It returns both in black background like cmd. I am using Jupyter Notebook btw. I want the data frames to be returned in proper table format. Commented Sep 29, 2018 at 12:43

2 Answers 2

30

How about this:

def test():
    df1 = pd.DataFrame([1,2,3], ['a','b','c'])
    df2 = pd.DataFrame([4,5,6], ['d','e','f'])
    return df1, df2

a, b = test()
display(a, b)

This prints out:

    0
a   1
b   2
c   3

    0
d   4
e   5
f   6
Sign up to request clarification or add additional context in comments.

Comments

-1

Call IPython (module) in it display(module) and then import display(function) from it

from IPython.display import display

Comments

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.