Edit long format multi-format formats with Pandas

I would like to turn:

DateTime                     ColumnName        Min      Avg      Max                                                                                      
2012-10-14 11:29:23.810000   Percent_Used       24       24       24
2012-10-14 11:29:23.810000   Current_Count  254503   254503   254503
2012-10-14 11:29:23.810000   Max           1048576  1048576  1048576
2012-10-14 11:34:23.813000   Percent_Used       24       24       24
2012-10-14 11:34:23.813000   Current_Count  254116   254116   254116
2012-10-14 11:34:23.813000   Max           1048576  1048576  1048576

In a data frame where DateTimes are unique (index) and columns:

DataTime, Percent_Used_Min, Percent_Used_Avg, Percent_Used_Max, Current_Count_Min, Current_Count_Avg, Current_Count_Max, Max_Min, Max_Avg, Max_Max

Basically, I want to simulate R melt / cast without falling into hierarchical indexing or stacked data frames. I can't seem to accurately reproduce the above game with stack / unpack, melt or turntable / turntable. Is there a good way to do this?

As an example, in R, it will be something like:

dynamic_melt = melt(dynamic, id = c("DateTime", "ColumnName"))
recast = data.frame(cast(dynamic_melt, DateTime ~ ...))

The above data will be variables (i.e. ColumnName values ​​will not always be the same, there may be more or less, as well as different names).

+2
1

pandas.core.reshape melt:

In [52]: melted = reshape.melt(df, id_vars=['DateTime', 'ColumnName'])

In [53]: melted.set_index(['DateTime', 'ColumnName', 'variable']).value.unstack([1, 2])
Out[53]: 
ColumnName                  Percent_Used  Current_Count      Max  Percent_Used  Current_Count      Max  Percent_Used  Current_Count      Max
variable                             Min            Min      Min           Avg            Avg      Avg           Max            Max      Max
DateTime                                                                                                                                    
2012-10-14 11:29:23.810000            24         254503  1048576            24         254503  1048576            24         254503  1048576
2012-10-14 11:34:23.813000            24         254116  1048576            24         254116  1048576            24         254116  1048576

MultiIndex, , .

+7

Source: https://habr.com/ru/post/1667613/


All Articles