Jesper Holm-Pedersen
Jesper Holm-Pedersen

Reputation: 23

Calculate a sum on a range in column in pandas

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.

Upvotes: 2

Views: 586

Answers (3)

Quinten
Quinten

Reputation: 41357

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

Upvotes: 1

Shah
Shah

Reputation: 130

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

Upvotes: 1

Timeless
Timeless

Reputation: 37787

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

Upvotes: 2

Related Questions