Home > database >  Accessing groupby value based on id?
Accessing groupby value based on id?

Time:05-14

I have an array which has id and weight information, I want to groupby the array based on the id regarding min and max weight:

a= pd.Series(weight).groupby(id).agg(["min", "max"])

I want to access each min/ max element based on their id. My question is how to save and access each min/max value based on keys(id) so that I can access each element?

I use w_min=a.iloc[0,0] to access each element, what is the best way of accessing each element?

CodePudding user response:

If I understand you correctly, you can use .to_dict() and then you can access your values by key (in this case id):


#... code as in your question

out = a.to_dict(orient="index")

print(out)
print(out[5]["min"])  # <-- access by `5` and `min`

Prints:

{
    1: {"min": 100, "max": 200},
    3: {"min": 258, "max": 585},
    5: {"min": 89, "max": 632},
}
89
  • Related