3

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)
1
  • @MaxU I receive the following error message:SyntaxError: invalid character in identifier Commented Dec 11, 2016 at 21:50

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

11 Comments

This solution is much better ;)
@MaxU, Thanks for that.
@MCM, you can simply replace df with your "DataLevels": pd.melt(DataLevels['Adj Close'].reset_index(), id_vars='Date', value_name='Adj Close', var_name='Minor')
@MCM, If you want to rename the column after melting, you could then do - pd.melt(DataLevels['Adj Close'].reset_index(), id_vars=['Date'], value_name='Adj Close', var_name='Minor').rename(columns={'Minor': 'Ticker'}). Since you haven't accepted answers before, you may want to do that so that it indicates to the wider community that this solution has solved your problem successfully.
@NickilMaveli Thanks a lot. I just figured it faster out as I could delete my post :). I think the post can be closed.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.