I'm trying to apply a function to a DataFrame at row wise(axis = 1), and when the applied function return a series, the final returned value of 'apply' would be a dataframe, which is not what I want. I've find a similar problem here, Returning multiple values from pandas apply on a DataFrame, however this case is about applying function to a groupby. and in the case of non-group, a dataframe will be returned even if the returned series of applied function are with different length.
In [10]: import pandas as pd
In [11]: import numpy as np
In [12]: df = pd.DataFrame({'start': [1, 2, 3], 'end': [7, 9, 9]})
In [13]: df
Out[13]:
end start
0 7 1
1 9 2
2 9 3
In [14]: def fun(df):
...: return pd.Series(np.arange(df['start'], df['end'], 1))
...:
In [15]: df.apply(fun, axis=1)
Out[15]:
0 1 2 3 4 5 6
0 1.0 2.0 3.0 4.0 5.0 6.0 NaN
1 2.0 3.0 4.0 5.0 6.0 7.0 8.0
2 3.0 4.0 5.0 6.0 7.0 8.0 NaN
however, what I want is something like this(a hierarchical series):
Out[23]:
0 0 1.0
1 2.0
2 3.0
3 4.0
4 5.0
5 6.0
1 0 2.0
1 3.0
2 4.0
3 5.0
4 6.0
5 7.0
6 8.0
2 0 3.0
1 4.0
2 5.0
3 6.0
4 7.0
5 8.0
dtype: float64