Search code examples
python-3.xpandaspandas-groupbypandas-datareader

Should I stack, pivot, or groupby?


I'm still learning how to play with dataframe and still can't make this... I got a dataframe like this:

A  B  C  D1 D2 D3
1  2  3  5  6  7 

I need it to look like:

A  B  C  DA D 
1  2  3  D1 5 
1  2  3  D2 6 
1  2  3  D3 7 

I know I should use something like groupby but I still can't find good documentation.


Solution

  • This is wide_to_long

    ydf=pd.wide_to_long(df,'D',i=['A','B','C'],j='DA').reset_index()
    ydf
       A  B  C  DA  D
    0  1  2  3   1  5
    1  1  2  3   2  6
    2  1  2  3   3  7