Search code examples
pythonpandasdataframedatareader

Convert frame pandas in python


I receive the output from my pandas datareader as the following:

       Date        AAPL         ACN
0 2016-11-21  111.730003  119.680000
1 2016-11-22  111.800003  119.480003
2 2016-11-23  111.230003  119.820000
3 2016-11-25  111.790001  120.739998
4 2016-11-28  111.570000  120.760002

What I that the frame looks like:

       Date   Minor   Adj Close
0 2016-11-21  AAPL    111.730003  
1 2016-11-22  AAPL    111.800003  
2 2016-11-23  AAPL    111.230003  
3 2016-11-25  AAPL    111.790001  
4 2016-11-28  AAPL    111.570000  
0 2016-11-21  ACN     119.680000
1 2016-11-22  ACN     119.480003
2 2016-11-23  ACN     119.820000
3 2016-11-25  ACN     120.739998
4 2016-11-28  ACN     120.760002

How can I converte my code that I receive the output mentioned above? The code looks like:

import pandas_datareader.data as pdr

DataLevels = pdr.DataReader(['ACN','AAPL'], 'yahoo', '2016-11-19', '2016-12-1')
DataLevels = DataLevels['Adj Close'].reset_index()
DataLevels.rename(columns={'minor': ['ACN','AAPL'], 'Adj Close': 'Adj Close'}, inplace=True)
print(DataLevels)

Solution

  • Use melt to reshape the DF from wide to long format:

    pd.melt(df, id_vars=['Date'], value_name='Adj Close', var_name='Minor')
    

    enter image description here