Search code examples
pythonfunctiondataframedata-extraction

Create function to extract data from dataframe based on value


I have a dataframe containing e-sports game data:

index   result   map_index   map_games_total   map_win_rate   
0       win      1           3                 0.66           
1       win      3           2                 0.5            
2       loss     3           2                 0.5            
3       loss     2           1                 0.0           
4       win      1           3                 0.66           
5       loss     1           3                 0.66          
6       win      4           1                 1.0            

I would like to define a function that automatically identifies the three most played maps (map = map_index) and creates a new dataframe containing only the rows for those maps for each of the identified maps. So for the example above that would be something like this:

subdf_1

index   result   map_index   map_games_total   map_win_rate   
    0       win      1           3                 0.66           
    4       win      1           3                 0.66           
    5       loss     1           3                 0.66           

subdf_2

 index   result   map_index   map_games_total   map_win_rate   
    1       win      3           2                 0.5            
    2       loss     3           2                 0.5            

subdf_3

 index   result   map_index   map_games_total   map_win_rate   
    3       loss     2           1                 0.0            

subdf_4

 index   result   map_index   map_games_total   map_win_rate   
    6       win      4           1                 1.0            

Thank you.


Solution

  • try this,

    results = {}
    
    for i, j in enumerate(df.map_win_rate.unique()):
        results[f'subdf_{i}'] = df[df.map_win_rate.eq(j)]
    

    print(results['subdf_1'])
    
       index result  map_index  map_games_total  map_win_rate
    1      1    win          3                2           0.5
    2      2   loss          3                2           0.5
    
    print(results['subdf_2'])
    
       index result  map_index  map_games_total  map_win_rate
    3      3   loss          2                1           0.0
    
    ...