简体   繁体   中英

How to eliminate negative values and calculate the sum of just positive integers?

I have an excel sheet to export the data from, I need to sum two columns individually and divide them to get the answer. but, while doing sum operation I don't want my code to consider the negative values from 'YTD chg hrs actual' column (ie, I just wanted to sum only positive values). how could I achieve this from the following code?

util = "my file location goes here"
utilization_by_region=(((util.groupby(['BA Name','PC Name'])['YTD Chg Hrs Actual']).sum())/(util.groupby(['BA Name','PC Name'])['YTD Normal Hrs Actual'].sum())).replace(np.inf,np.nan)
utilization_by_service_line=((util.groupby(['PC Name','BA Name'])['YTD Chg Hrs Actual'].sum())/(util.groupby(['PC Name','BA Name'])['YTD Normal Hrs Actual'].sum())).replace(np.inf,np.nan)
print(utilization_by_region.fillna(0),utilization_by_service_line.fillna(0))

Imagine you have a dataframe named df , and it has a column of integer numbers. This code will help you to compute the summation of only positive values.

# df is your dataframe, 'A' is the column.
sum = df[df['A']>0].sum()

you can find positive numbers indexes and only compute the summation of the resulted subframe.

For example purpose let's create the following DataFrame:

df = pd.DataFrame({'A': [ 2.5, 3.5, -10.1 -7.5, 3.0 ],
    'B': [ 3.5, -10.2 -7.8, 0.5, -0.1 ]})

Then, to leave only positive values, create an auxiliary DataFrame:

df2 = df.where(df > 0, 0)

Then, to compute sum(A) / sum(B), execute:

df2.A.sum() / df2.B.sum()

For the above example data, the result is 2.25 .

Now change column names to your columns and you have your result.

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