Articles

sum in pandas | mean in pandas dataframe

sum in pandas | mean in pandas dataframe


In this pandas tutorial, we will discuss about:

  • sum in pandas dataframe,
  • mean in pandas dataframe

Lets first create a dataframe.

 

DataFrame is an two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.

 

We can able to create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.

Syntax:

pandas.DataFrame(data)

Where, data is the input dataframe. The data can be a dictionary that stores list of values with specified key.

 

Example: Create Dataframe

In this example, we will create dataframe with 4 rows and 4 columns with college data and assign index labels using index parameter.

import pandas as pd

#create dataframe from the college data

data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['college1','college2','college3','college4'])



#display the dataframe

print(data)

Output:

create dataframe pandas


sum in pandas dataframe

sum() function is used to get the return the total (sum) value from the pandas dataframe.

Syntax:

dataframe['column'].sum()

where,

  • dataframe is the input dataframe
  • column is the column name in pandas DataFrame.

Example : sum of columns in pandas dataframe

In this example, we are applying sum() function on Total Staff column to find sum in pandas dataframe.

import pandas as pd

#create dataframe from the college data

data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['college1','college2','college3','college4'])


#get the sum  from Total Staff column in the dataframe

print(data['Total Staff'].sum())

 

Output: Result for sum in pandas dataframe is given below for total staff column

10936

The above code will return total sum from Total Staff column.


mean in pandas dataframe

mean() function is used to get the return the average value from the pandas dataframe.

Syntax:

dataframe['column'].mean()

where,

  • dataframe is the input dataframe
  • column is the column name in pandas DataFrame.

Example : mean of columns in pandas dataframe

In this example, we are applying mean() function on Total Staff column to find mean in pandas dataframe.

import pandas as pd

#create dataframe from the college data

data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['college1','college2','college3','college4'])



#get the mean  from Total Staff column in the dataframe
print(data['Total Staff'].mean())

Outputmean of columns in pandas dataframe for total staff column is given below

2734.0

The above code will return average (mean) value  from Total Staff column


Conclusion

In this article, we discussed how to return sum and average from particular column in Pandas DataFrame using sum()  and mean() functions.


Pandas

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1200+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Mar 12,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!