简体   繁体   中英

How do you sum a dataframe based off a grouping in Python pandas?

I have a for loop with the intent of checking for values greater than zero.

Problem is, I only want each iteration to check the sum of a group of ID's.

The grouping would be a match of the first 8 characters of the ID string.

I have that grouping taking place before the loop but the loop still appears to search the entire df instead of each group.

LeftGroup = newDF.groupby(‘ID_Left_8’)
for g in LeftGroup.groups:
     if sum(newDF[‘Hours_Calc’] > 0):
     print(g)

Is there a way to filter that sum to each grouping of leftmost 8 characters?

I was expecting the.groups function to accomplish this, but it still seems to search every single ID.

Thank you.

def filter_and_sum(group):
    return sum(group[group['Hours_Calc'] > 0]['Hours_Calc'])

LeftGroup = newDF.groupby('ID_Left_8')
results = LeftGroup.apply(filter_and_sum)
print(results)

This will compute the sum of the Hours_Calc column for each group, filtered by the condition Hours_Calc > 0 . The resulting series will have the leftmost 8 characters as the index, and the sum of the Hours_Calc column as the value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM