3

I'm trying to reshape my long data to a wide format. The data currently looks like:

OBS . date . TICKER . RET

1 . 20050131 . AAPL . 0.02
2 . 20050231 . AAPL . 0.01
3 . 20050131 . GOOG . 0.05
4 . 20050231 . GOOG . 0.03

And I would like to get the data like:

TICKER . 20050131 . 20050231

AAPL   .   0.02   .   0.01
GOOG   .   0.05   .   0.03

The data is stored in a pandas dataframe. I tried stacking the data, but I don't think I'm doing it right.

Thanks for the help!

SK23
  • 85
  • 1
  • 1
  • 4

1 Answers1

7

You can pivot your dataframe:

df.pivot(index='TICKER', columns='date', values='RET')

date    20050131  20050231
TICKER                    
AAPL        0.02      0.01
GOOG        0.05      0.03
sacuL
  • 49,704
  • 8
  • 81
  • 106
  • I tried to do this but I get the following error: "ValueError: Index contains duplicate entries, cannot reshape" – SK23 Mar 25 '18 at 22:06
  • You'll need to think about what you want to achieve in that case: if youo have multiple entries for *e.g.* `AAPL` on a single date, what value would you want there? `df.pivot` can't choose that for you. – sacuL Mar 25 '18 at 22:10
  • Oh, so that error is caused by having multiple entries of the same date? I guess I'll have to clean up the data first then. Thanks for the help! – SK23 Mar 25 '18 at 22:45