Panda dataframe conversion into table

时间:2019-05-31 11:33:44

标签: pandas dataframe pandas-groupby

After applying the group by I got a data frame like this

                                 indi_percentage
publishedAt status                     
2019-05-29  Guidelines reason        14.065392
            accepted                 0.555213
            unfit                    0.246761
            repeat                   0.123381
2019-05-30  Guidelines reason        11.217712
            accepted                 0.073801
            unfit                    0.221402
            no action                90.943396

The shape of the data frame is 8 X 1. The indi_percentage is a column. Rest are index. I want to transform this data frame in such a way that on each day, values of status are columns and indi_percentage are the values these columns. Here is a sample output

publishedAt   Guidelines reason     accepted
2019-05-29     14.065392            0.555213

How can we implement this?

1 个答案:

答案 0 :(得分:2)

reset_index() and df.pivot_table() assuming df is the name after grouby()

df.reset_index().pivot_table(index='publishedAt',columns='status',values='indi_percentage')

 status       Guidelines reason  accepted  no action    repeat     unfit
publishedAt                                                            
2019-05-29           14.065392  0.555213        NaN  0.123381  0.246761
2019-05-30           11.217712  0.073801  90.943396       NaN  0.221402