Home > Net >  Calculate a sum on a range in column in pandas
Calculate a sum on a range in column in pandas

Time:10-01

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.

CodePudding user response:

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

CodePudding user response:

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
  • Related