2

I am quite new but I am working with a dataframe that looks like the below:

ID       Tag  
123      Asset Class > Equity  
123      Inquiry Type > Demonstration
123      Inquiry Topic > Holdings  
456      Asset Class > Fixed Income  
456      Inquiry Type > BF   
456      Inquiry Topic > VaR 

What I am trying to do is change this into a dataframe which looks like the below:

ID       Asset Type      Inquiry Type      Inquiry Topic  
123      Equity          Demonstration     Holdings
456      Fixed Income    Bf                VaR 

Sorry if this is quite simple; however, I am having an issue with this manipulation. I have tried .melt but this does not seem to complete what I am trying to complete.

1 Answer 1

1

Use split with pivot and selecting splitted lists by indexing:

s = df['Tag'].str.split(' > ')
df = (pd.pivot(index=df['ID'], columns=s.str[0], values=s.str[1])
       .reset_index()
       .rename_axis(None, 1))
print (df)
    ID   Asset Class Inquiry Topic   Inquiry Type
0  123        Equity      Holdings  Demonstration
1  456  Fixed Income           VaR             BF
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for the help!

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.