O'Reilly logo

Mastering pandas by Femi Anthony

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Pivots and reshaping data

This section deals with how you can reshape data. Sometimes, data is stored in what is known as the stacked format. Here is an example of a stacked data using the PlantGrowth dataset:

In [344]: plantGrowthRawDF=pd.read_csv('./PlantGrowth.csv')
       plantGrowthRawDF
Out[344]:     observation   weight  group
         0    1             4.17    ctrl
         1    2             5.58    ctrl
         2    3             5.18    ctrl
         ...
         10   1             4.81    trt1
         11   2             4.17    trt1
         12   3             4.41    trt1
        ... 
         20   1             6.31    trt2
         21   2             5.12    trt2
         22   3             5.54    trt2

This data consists of results from an experiment to compare dried weight yields of plants that were obtained under a control (ctrl) and two different treatment conditions (trt1, trt2). Suppose we wanted to do some analysis of this data by their group value. One way to do this would ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required