I can't figure out a way to easity have new columns corresponding to some rows in a dataframe.
Let's take following pandas data frame :
df = pandas.DataFrame({'date':[13,13,13,13],'city':['NY','NY','SF','SF'],'timeOfTheDay':['day','night','day','night'],'count':[2,3,4,5]})
Output :
date | city | timeOfTheDay | count |
---|---|---|---|
13 | NY | day | 2 |
13 | NY | night | 3 |
13 | SF | day | 4 |
13 | SF | night | 5 |
What I would like to get :
date | city | countDay | countNight |
---|---|---|---|
13 | NY | 2 | 3 |
13 | SF | 4 | 5 |
I tried to use pivot_table :
df.pivot_table('count',['date','city'],'timeOfTheDay')
Or similarly :
pandas.pivot_table(df,index=['date','city'],columns=['timeOfTheDay'],values=['count'])
And instead of expected result it got me :
timeOfTheDay day night
date city
13 NY 3.0 NaN
SF NaN 4.0