1
        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?

5
  • Did you concatenate two DataFrames to get this? If so, add an axis=1 parameter to pd.concat. Commented Feb 25, 2019 at 23:11
  • Maybe, I am directly pulling this using an API where I specify the different fields needed . I believe what happens is, fields having similar frequency are returned in a single dataframe, the sector column is data at different frequency , and the API function just concatenates them together as you suggested Commented Feb 25, 2019 at 23:15
  • Do you have control over the API? Because this should be changed there... Commented Feb 25, 2019 at 23:22
  • 1
    In the meantime, will df.groupby(level=0).first() work? Commented Feb 25, 2019 at 23:24
  • Unfortunately cant control the API,but what you suggested does work. Thanks a lot for your help. Commented Feb 25, 2019 at 23:27

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

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.