Thedudeabides
Thedudeabides

Reputation: 93

Pandas dataframe reorienting

        TA    Sector GP     OPR
ID              
AAPL    0.03   NaN   0.20   0.10
MSFT    0.04   NaN   0.30   0.05
AAPL    NaN    IT    NaN     NaN
MSFT    NaN    IT    NaN     NaN

I would like to convert the above frame into

Ticker Sector TA GO OPR
AAPL   IT      0.03 0.20 0.10
MSFT   IT      0.04 0.30 0.05

Is there a straight forward way to accomplish this?

Upvotes: 1

Views: 168

Answers (1)

cs95
cs95

Reputation: 402844

This should be handled upstream if possible, but in the meantime the bandaid is using groupby on the index with first:

df.groupby(level=0).first()

        TA Sector   GP   OPR
ID                          
AAPL  0.03     IT  0.2  0.10
MSFT  0.04     IT  0.3  0.05

Upvotes: 2

Related Questions