Calculate a sum on a range in column in pandas

Question:

I would like to calculate a sum on a range in column in panda dataframe in python.

Panda DataFrame:

acc     accname     amount
1010    turnover    10000
2000    salaries    -4000
3000    rent        -1500
5000    assets      15000
6000    liabilities -15000

I would like to calculate the result. 10000-4000-1500 =4500 or sum(1010:3000).
And I would like to define the value as a variable called result.

Answers:

You can use pandas.DataFrame.set_index and pandas.DataFrame.loc :

result = df.set_index('acc').loc[1010:3000, 'amount'].sum()

# Output :

print(result)
4500
Answered By: abokey

I am adding the whole data frame make and print to understand how its working

data = {'acc':[1010,2000,3000,5000,6000],
        'accname':['turnover','salaries','rent','asstes','liablities'],
        'amount':[10000,-4000,-1500,15000,-15000]
}
df = pd.DataFrame(data)
print(df)

You can use it simply by

result  = df['amount'].sum()
print(result)

output is:

4500
Answered By: Shah

Another option using a range of values for a certain column like this:

result = df[(df.acc >= 1010) & (df.acc <= 3000)]['amount'].sum()

Output:

4500
Answered By: Quinten
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.