2

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.

Timeless
  • 22,580
  • 4
  • 12
  • 30

3 Answers3

2

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
Timeless
  • 22,580
  • 4
  • 12
  • 30
1

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
Shah
  • 120
  • 1
  • 9
  • Thank you. The result is correct, but only because 5000 and 6000 sums to zero. I need a range in order to it make it work. I have received two solutions that solves that. – Jesper Holm-Pedersen Oct 01 '22 at 14:09
1

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
Quinten
  • 35,235
  • 5
  • 20
  • 53