Search code examples
pythonpandasdataframeyahoo

Changing value in dataframe to column


I retrieve the yahoo finance fundamental data using yahoo_fin api on Return on Asset (ROA) and Return of Equity (ROE) which gives me the layout format:

   Symbol   Attribute   Value   
0       A        ROA       X%
1       A        ROE      XX%
2       B        ROA       Y%
3       B        ROE      YY%
4       C        ROA       Z%
5       C        ROE      ZZ% 

I did df.transpose() but gives me the following layout format:

                  0    1    2    3    4    5
Symbol            A    A    B    B    C    C
Attribute         ROA  ROE  ROA  ROE  ROA  ROE 
Value             X%   XX%  Y%   YY%  Z%   ZZ%

The ROA and ROE is represented as value in the df, my goal is to shift these value to column . My ideal layout format:

Symbol   ROA   ROE
A        X%    XX%
B        Y%    YY%
C        Z%    ZZ%

I know how to set the index on symbol but would need to first change the value in the df to column. Appreciate any advice, thanks for the help.


Solution

  • you can use pivot

    df.pivot('Symbol','Attribute', 'Value')
    

    output

    Attribute   ROA ROE
    Symbol      
    A   X%  XX%
    B   Y%  YY%
    C   Z%  ZZ%