Search code examples
pythonpandasdataframefor-loopunique

Create few Data Frames from existing Data Frames based on unique values


My DF looks like below

x  y  z  b
1  2  3  Max
12 32 8  Max
1  2  3  Jon
12 32 8  Max
1  25  3  Jon
12 32 81  Anna

So I need to based on column b, take unique values (in this case: Max, Jon, Anna) and create 3 new df like this:

df_1:

x  y  z  b
1  2  3  Max
12 32 8  Max
12 32 8  Max

df_2:

x  y  z   b
1  2  3   Jon
1  25  3  Jon

df_3:

x  y  z   b
12 32 81  Anna

I was looking for the answer but I don't know how can I create new DF's. Do you have any ideas? Of course in original DF there is more unique values.

Regards Tomasz


Solution

  • Use locals() to create variable dynamically:

    Update

    Do you have maybe idea how instead of calling DF: DF_1, DF_2, DF_3 using unique names? I mean DF_Max, DF_Jon, DF_Anna and save every DF into excel?

    for name, subdf in df.groupby('b', sort=False):
        locals()[f'df_{name}'] = subdf
        subdf.to_excel(f'{name}.xlsx', index=False)
    
    >>> df_Max
        x   y  z    b
    0   1   2  3  Max
    1  12  32  8  Max
    3  12  32  8  Max
    
    
    >>> df_Jon
       x   y  z    b
    2  1   2  3  Jon
    4  1  25  3  Jon
    
    
    >>> df_Anna
        x   y   z     b
    5  12  32  81  Anna
    

    Old answer

    for i, (_, subdf) in enumerate(df.groupby('b', sort=False), 1):
        locals()[f'df_{i}'] = subdf
    
    >>> df_1
        x   y  z    b
    0   1   2  3  Max
    1  12  32  8  Max
    3  12  32  8  Max
    
    >>> df_2
       x   y  z    b
    2  1   2  3  Jon
    4  1  25  3  Jon
    
    >>> df_3
        x   y   z     b
    5  12  32  81  Anna
    

    https://stackoverflow.com/a/68969956/15239951

    https://stackoverflow.com/a/68268034/15239951