Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return two data frames from a function with data frame format

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.

like image 829
luminare Avatar asked Sep 29 '18 12:09

luminare


People also ask

Can you Rbind multiple data frames?

The rbind() function in R and the bind_rows() function are the most useful functions when it comes to data manipulation. You can easily bind two data frames of the same column count using rbind() function.

How do you use two data frames?

The concat() function in pandas is used to append either columns or rows from one DataFrame to another. The concat() function does all the heavy lifting of performing concatenation operations along an axis while performing optional set logic (union or intersection) of the indexes (if any) on the other axes.


1 Answers

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
like image 103
petezurich Avatar answered Oct 18 '22 05:10

petezurich