Home > database >  Creating a dictionary from unique entries in a pandas data frame
Creating a dictionary from unique entries in a pandas data frame

Time:05-23

I have a certain pandas data frame that looks like this:

x y unit ID leak/min
0 12 34 1 3.3
1 99 42 1 2.33
2 45 55 3 3.11

It runs upto 400 entries.

I would like to create a function that determines the number of unique units and then return a dictionary with subsets of the data frame. So the key is a string with unit IDs and each value is the dataframe of a subset of the original data frame.

I know how to find the unique values using this code; uniqueValues = data['ID'].unique() but having problems implementing the dictionary.

CodePudding user response:

IIUC you want something like this:

dict_dfs = dict(tuple(df.groupby("unit ID")))

for key, val in dict_dfs.items():
    print(f"{key=}: \n {val} \n")

key=1: 
     x   y  unit ID  leak/min
0  12  34        1      3.30
1  99  42        1      2.33 

key=3: 
     x   y  unit ID  leak/min
2  45  55        3      3.11 
  • Related