sasoptpy.flatten_frame

flatten_frame(df, swap=False)[source]

Converts a pandas.DataFrame object into pandas.Series

Parameters
dfpandas.DataFrame

DataFrame to be flattened

swapboolean, optional

Option to use columns as first index

Returns
new_framepandas.DataFrame

A new DataFrame where indices consist of index and columns names as tuples

Examples

>>> price = pd.DataFrame([
>>>     [1, 5, 7],
>>>     [8, 4, 3],
>>>     [5, 7, 9]], columns=['period1', 'period2', 'period3']).\
>>>     set_index([['coal', 'steel', 'copper']])
>>> print('Price data: \n{}'.format(price))
>>> price_f = so.flatten_frame(price)
>>> print('Price data: \n{}'.format(price_f))
Price data:
        period1  period2  period3
coal          1        5        7
steel         8        4        3
copper        5        7        9
Price data:
(coal, period1)      1
(coal, period2)      5
(coal, period3)      7
(steel, period1)     8
(steel, period2)     4
(steel, period3)     3
(copper, period1)    5
(copper, period2)    7
(copper, period3)    9
dtype: int64