Luis Medina
Luis Medina

Reputation: 555

Pandas grouped index to separate columns with respective values

I'm trying to convert a grouped timeindex dataframe, so each index is a new column and the columns have the data that used to correspond to each index, how can I do this? this is the example:

data = {'field1':['a','a','c','a','b','c','a','b','c','a','b','c','c'],
       'field2':[1,5,12,10,8,4,33,9,1,33,9,1,1],
}

df = pd.DataFrame(data)
df = pd.DataFrame(data, index =['2020-01-01 06:00:00-05:00', '2020-01-01 06:20:00-05:00', '2020-01-01 06:28:00-05:00',
                                '2020-01-01 06:25:00-05:00', '2020-01-01 07:00:00-05:00', '2020-01-01 07:09:00-05:00',
                                '2020-01-01 07:15:00-05:00','2020-01-01 07:48:00-05:00', '2020-01-01 06:20:00-05:00',
                               '2020-01-01 08:33:00-05:00','2020-01-01 08:38:00-05:00','2020-01-01 06:20:00-05:00',
                               '2020-01-01 08:45:00-05:00'])
df.index = pd.to_datetime(df.index)

df=df.groupby([pd.Grouper(freq='1H'), 'field1']).count()

enter image description here

and I want to convert it in something like this: enter image description here

Upvotes: 1

Views: 35

Answers (1)

wwnde
wwnde

Reputation: 26676

You were almost there.Just .unstack() AND transpose the resulting dataframe

  df=df.groupby([pd.Grouper(freq='1H'), 'field1']).count().unstack()\
.T.reset_index().drop(columns='level_0')



  field1  2020-01-01 06:00:00-05:00  2020-01-01 07:00:00-05:00  \
0      a                        3.0                        1.0   
1      b                        NaN                        2.0   
2      c                        3.0                        1.0   

   2020-01-01 08:00:00-05:00  
0                        1.0  
1                        1.0  
2                        1.0  

Upvotes: 1

Related Questions