Home > Net >  Check internal condition of a groupby
Check internal condition of a groupby

Time:03-18

I have a list of ids as follows:

new_list = ['id1', 'id2', 'id3']

And I want to go through the list and perform for each id a groupby of its columns. The select_subset_interval function returns a specific dataframe for the value of i. The implementation of this function is outside the scope of the question.

for i in new_list:
   print("**",i,"**")
   subset = select_subset(i)
   print(subset.groupby(['zone','type'])['counter'].sum())

** id1 **
zone                        type
Hall                        IN      245
                            OUT     0
Room                        IN      165

** id2 **
zone                        type
Pool                        IN      10
                            OUT     301

** id3 **
zone                        type
Room                        IN      165
                            OUT     0

I would like the function to only display groupby's whose counter variable sum is 0 for at least one type value. That is, for the example above I just want it to show the following result:

** id1 **
zone                        type
Hall                        IN      245
                            OUT     0
Room                        IN      165


** id3 **
zone                        type
Room                        IN      165
                            OUT     0

I only want to print the groupby for identifiers that contain at least one row with the value zero for their counter variable once summed.I hope you can help me.

Example of my dataframe:

subset por i = 'id1'

ID type zone counter
id1 IN Hall 245
id1 OUT Hall 0
id1 IN Room 160
id1 IN Room 5

subset por i = 'id2'

ID type zone counter
id2 IN Pool 10
id2 OUT Pool 301
id2 OUT Pool 0

subset por i = 'id3'

ID type zone counter
id3 OUT Room 0
id3 OUT Room 0
id3 IN Room 140
id3 IN Room 25

CodePudding user response:

Use the following code in your function to return the desired output:

temp = df.groupby(['type', 'zone'])['counter'].sum()
if temp.min() == 0:
    return temp
  • Related