Search code examples
python-3.xpandasfeature-extractionfeature-selectionfeature-engineering

How to unstack observations and arrange them in columns


I have a dataframe like below, the row number is identical with all XX YY ZZ variables,

Name date    Value 
XX   01/20     69
XX   02/20     75
YY   01/20     450
YY   02/20     430
ZZ   01/20     1000
ZZ   02/20     899

how to convert it into

date      XX   YY   ZZ 
01/20     69  450   1000
02/20     75  430   899

You notice that value column is gone, and all values became observations under XX YY ZZ Features.


Solution

  • you can use pivot:

    df.pivot(index='date', columns='Name', values='Value')