0

I have a dataframe that looks like the below:

 | Day    | Hour  |  Count
 |--------|-------|--------
0| Monday | 01    | 10
1| Monday | 03    | 2
2| Monday | 04    | 6
3| Monday | 07    | 8
4| Monday | 08    | 3
5| Tuesday| 01    | 7
6| Tuesday| 02    | 6
7| Tuesday| 04    | 2
8| Tuesday| 07    | 8
9| Tuesday| 08    | 5

I would like to transform it into the below:

 | Day    |  01  |  02  |  03  |  04  |  05  |  06  |  07  |  08  |    
 |--------|------|------|------|------|------|------|------|------|
0| Monday |  10  |  NaN |  2   |  6   |  NaN |  NaN |  8   |  3   | 
1| Tuesday|  7   |  6   |  NaN |  2   |  NaN |  NaN |  8   |  5   | 

I'm not sure if there is a term for this. However, I have been struggling and would appreciate some help.

Thanks so much!

  • 1
    Use [`.pivot`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.pivot.html) – Andrej Kesely Aug 06 '21 at 10:10
  • Just for your reference the your solution will look something like this ```pd.pivot_table(df, values = 'Count', index=['Day'], columns = 'Hour').reset_index()``` – A DUBEY Aug 06 '21 at 11:53

0 Answers0