Pan Kevin
Pan Kevin

Reputation: 55

How to transform the column‘s values to spread the Dataframe?

Now I've got a Dataframe like this:

Stock  Price Change
Date                          
2015-12-31  COVS           NaN
2016-01-04  COVS      0.000000
2016-01-05  COVS     -0.056000
2016-01-06  COVS     -0.008475
2016-01-07  COVS     -0.025641
2016-01-08  COVS     -0.052632
2015-12-31  EMMS           NaN
2016-01-04  EMMS      0.015625
2016-01-05  EMMS     -0.030769
2016-01-06  EMMS      0.079365
2016-01-07  EMMS     -0.029412
2016-01-08  EMMS     -0.045455
2015-12-31   VOD           NaN
2016-01-04   VOD     -0.002170
2016-01-05   VOD      0.001553
2016-01-06   VOD     -0.009615
2016-01-07   VOD     -0.009396
2016-01-08   VOD      0.028770

And my problem is simple, How can I transform it into this:

Stock              COVS       EMMS        VOD         
Date     
2015-12-31         NaN         NaN        NaN
2016-01-04      0.000000     0.015625  -0.002170
2016-01-05     -0.056000    -0.030769   0.001553
2016-01-06     -0.008475     0.079365  -0.009615
2016-01-07     -0.025641    -0.029412  -0.009396
2016-01-08     -0.052632    -0.045455   0.028770

Since this kind of problem is typical, I'm sure I'll encounter more than once.

And I've used the 'Dataframe.T' method, the result still doesn't satisfy me.

Willing to know your solutions.

Upvotes: 1

Views: 58

Answers (2)

dot.Py
dot.Py

Reputation: 5157

Take a look at Reshaping and Pivot Tables Docs.

You can use something like:

df.pivot(index='Date', columns='Stock', values='Price Change')

Example:

In [1]: df
Out[1]: 

         date variable     value
0  2000-01-03        A  0.469112
1  2000-01-04        A -0.282863
2  2000-01-05        A -1.509059
3  2000-01-03        B -1.135632
4  2000-01-04        B  1.212112
5  2000-01-05        B -0.173215
6  2000-01-03        C  0.119209
7  2000-01-04        C -1.044236
8  2000-01-05        C -0.861849
9  2000-01-03        D -2.104569
10 2000-01-04        D -0.494929
11 2000-01-05        D  1.071804

In [2]: df.pivot(index='date', columns='variable', values='value')
Out[2]: 

variable           A         B         C         D
date                                              
2000-01-03  0.469112 -1.135632  0.119209 -2.104569
2000-01-04 -0.282863  1.212112 -1.044236 -0.494929
2000-01-05 -1.509059 -0.173215 -0.861849  1.071804

Upvotes: 1

Grr
Grr

Reputation: 16099

Use the pivot method.

df.pivot('Date', 'Stock', 'Price Change')

Upvotes: 1

Related Questions