[Tutor] pandas data frame

Peter Otten __peter__ at web.de
Mon Jan 18 16:51:20 EST 2016


Bachir Bachir via Tutor wrote:

> Hi Peter Thank you much for your help its very appreciated ,bellow is an
> example of what i need.   The main dataframe ( need to be separated into
> separate dataframes  ). The desired dataframes output correctly as i need
> it to be,Thanks much Main dataframendx     V_id        Average        Mean
>       Peak 0         1              3                      2          51  
>       2              2                      1          62         3       
>       4                     1          83         1              2        
>              2          74         2              3                     3 
>         65         3              5                     3          46     
>    1              1                     1          87         2           
>   2                     5          108         3              5           
>          5          99         1              2                     5     
>     1010        2             5                     5          911       
> 3             4                     3          10 Dataframe-1ndx     V_id 
>  Average     Mean    Peak 0         1           3              2         
> 53         1           2              2          76         1           1 
>             1          89         1           2              5          10
> Dataframe-2ndx     V_id    Averag    Mean     Peak 1          2          
> 2              1          64          2           3              3        
>  67          2           2              5          1010        2          
> 5              5          9 Dataframe-3ndx     V_id    Average    Mean   
> Peak 2         3           4              1          85         3         
>  5              3          48         3           5              5        
>  911        3           4              3          10 Bachir

I see that you got an answer over at comp.python.pydata where you managed to 
get the format a bit more readable ;)

I recommend that you go with Goyo's answer, but instead of the hacky use of
locals() 
 
> >>> locals().update((('dataframe_{}'.format(k), grouper.get_group(k))
> ...                  for k in grouper.groups))

I recommend that you put the groups into a normal dict

groups = {k: g.get_group(k) for k in g.groups}

and then access the partial data frames with

v_id = 2
groups[v_id]



More information about the Tutor mailing list