In this post, we are to learn, How to sum specific rows in Pandas dataframe or Pandas sum specific rows. The DataFrame.eval() ,loc(),iloc[] and sum() functions are used with example.To use the Pandas library, first, we have to install it on the local system by using the pip command “pip install pandas” and import it into our code by using “import pandas as pd” to use its functions
1. How to sum specific rows Pandas dataframe
The python eval() function evaluates the given expression and executes it if it is a valid python statement. In this example, we have used the eval() function to pass a sum expression to sum specific columns Admit_fee+Tution_Fee + Fee function.
#python program to sum specifc rows in Pandas dataframe
import pandas as pd
data = {
'Name': ['Jack', 'Rack', 'Max', 'David'],
'Marks':[97,97,100,100],
'Admit_fee':[201,203,205,206],
'Fee':[100,200,300,400],
'Tution_Fee':[400,500,600,700]
}
dfobj = pd.DataFrame(data)
rows_sum = dfobj.eval('Sum = Admit_fee+Tution_Fee + Fee')
print(rows_sum)
Output
Name Marks Admit_fee Fee Tution_Fee Sum
0 Jack 97 201 100 400 701
1 Rack 97 203 200 500 903
2 Max 100 205 300 600 1105
3 David 100 206 400 700 1306
- Remove rows with duplicate indices Pandas
- Pandas sum rows by columns(6 ways)
- Pandas sum columns by multiple conditions
- How to Pandas sum all columns except one
2. How to sum specific rows Pandas dataframe
In the above example, we have used the eval() function along with the loc[] property to sum specific rows of Pandas dataframe. We have labeled the index of the dataframe by using the index =[‘R1′,’R2′,’R3′,’R4’]. The loc[] is used to select the rows specific rows and the eval() function to sum these rows by evaluating an expression.
import pandas as pd
data = {
'Name': ['Jack', 'Rack', 'Max', 'David'],
'Marks':[97,97,100,100],
'Admit_fee':[201,203,205,206],
'Fee':[100,200,300,400],
'Tution_Fee':[400,500,600,700]
}
dfobj = pd.DataFrame(data,index =['R1','R2','R3','R4'])
dfobj = dfobj.loc['R1':'R4'].eval('Sum = Admit_fee + Tution_Fee + Fee')
print(dfobj)
Output
Name Marks Admit_fee Fee Tution_Fee Sum
R1 Jack 97 201 100 400 701
R2 Rack 97 203 200 500 903
R3 Max 100 205 300 600 1105
R4 David 100 206 400 700 1306
3. How to sum specific rows Pandas dataframe using loc[]
The loc[] property is used to select the rows based on the rows label and the sum function is used to sum the selected specific rows. In this example, we have selected the rows based on the rows label using loc[‘R1′:’R4’] and the sum() function is to sum the rows.
import pandas as pd
data = {
'Name': ['Jack', 'Rack', 'Max', 'David'],
'Marks':[97,97,100,100],
'Admit_fee':[201,203,205,206],
'Fee':[100,200,300,400],
'Tution_Fee':[400,500,600,700]
}
dfobj = pd.DataFrame(data,index =['R1','R2','R3','R4'])
dfobj['Total'] = dfobj.loc['R1':'R4',['Admit_fee','Fee','Tution_Fee']].sum(axis = 1)
print(dfobj)
Output
Name Marks Admit_fee Fee Tution_Fee Total
R1 Jack 97 201 100 400 701
R2 Rack 97 203 200 500 903
R3 Max 100 205 300 600 1105
R4 David 100 206 400 700 1306
4. How to sum specific rows Pandas dataframe using iloc[]
The pandas iloc[] is an integer-based index used to select rows by position. While creating a dataframe pd.DataFrame(data) does not pass the index parameter, the default index starts from 0 to a number of rows. So we have used iloc[] to select the rows the index position and the sum() function to sum these specific rows.
import pandas as pd
data = {
'Name': ['Jack', 'Rack', 'Max', 'David'],
'Marks':[97,97,100,100],
'Admit_fee':[201,203,205,206],
'Fee':[100,200,300,400],
'Tution_Fee':[400,500,600,700]
}
dfobj = pd.DataFrame(data)
dfobj['Sum'] = dfobj.iloc[[1,3]].sum(axis=1)
print(dfobj)
Output
Name Marks Admit_fee Fee Tution_Fee Sum
0 Jack 97 201 100 400 NaN
1 Rack 97 203 200 500 1000.0
2 Max 100 205 300 600 NaN
3 David 100 206 400 700 1406.0
Summary
In this post, we have learned How to sum specific rows in Pandas dataframe using eval(),iloc[], and loc[] sum() function with examples.eval() evaluate a python expression to sum,iloc[] based on index, loc[] is used to sum based on row label.