Reputation: 60746
I have a Pandas Series where each element of the series is a one row Pandas DataFrame which I would like to append together into one big DataFrame. For example:
import pandas as pd
mySeries = pd.Series( numpy.arange(start=1, stop=5, step=1) )
def myFun(val):
return pd.DataFrame( { 'square' : [val**2],
'cube' : [val**3] } )
## returns a Pandas Series where each element is a single row dataframe
myResult = mySeries.apply(myFun)
so how do I take myResult
and combine all the little dataframes into one big dataframe?
Upvotes: 1
Views: 747
Reputation: 93804
concat them:
In [58]: pd.concat(myResult).reset_index(drop=True)
Out[58]:
cube square
0 1 1
1 8 4
2 27 9
3 64 16
Since the original indexes are all 0, I also reset them.
Upvotes: 2
Reputation: 879611
import pandas as pd
import numpy as np
mySeries = pd.Series(np.arange(start=1, stop=5, step=1))
def myFun(val):
return pd.Series([val ** 2, val ** 3], index=['square', 'cube'])
myResult = mySeries.apply(myFun)
print(myResult)
yields
square cube
0 1 1
1 4 8
2 9 27
3 16 64
Upvotes: 3
Reputation: 64443
Its seems overly complicated, although you probably posted a simplified example. Creating a new Series for each row creates a lot of overhead. This for example is over 200 times faster (for n=500) on my machine:
meResult = pd.DataFrame({'square': mySeries**2,'cube': mySeries**3})
Upvotes: 1