Reshaping Long Data to Wide in Python (Pandas)
15,547
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
Related videos on Youtube
Author by
SK23
Updated on June 04, 2022Comments
-
SK23 almost 2 years
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 about 6 yearsI tried to do this but I get the following error: "ValueError: Index contains duplicate entries, cannot reshape"
-
sacuL about 6 yearsYou'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. -
SK23 about 6 yearsOh, 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!