• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / pandas mean – Get Average of Series or DataFrame Columns

pandas mean – Get Average of Series or DataFrame Columns

January 11, 2022 Leave a Comment

To find the means of the columns in a DataFrame, or the average value of a Series in pandas, the easiest way is to use the pandas mean() function.

df.mean()

You can also use the numpy mean() function.

np.mean(df["Column"])

When working with data, many times we want to calculate summary statistics to understand our data better. One such statistic is the mean, or the average of a number.

Finding the mean of a column, or the mean for all columns or rows in a DataFrame using pandas is easy. We can use the pandas mean() function to find the average value of a column of numbers, or a DataFrame.

Let’s say we have the following DataFrame.

df = pd.DataFrame({'Age': [43,23,71,49,52,37], 
      'Test_Score':[90,87,92,96,84,79]})

print(df)
# Output: 
   Age  Test_Score
0   43          90
1   23          87
2   71          92
3   49          96
4   52          84
5   37          79

To get the means for all columns, we can call the pandas mean() function.

print(df.mean())

# Output:
Age           45.833333
Test_Score    88.000000
dtype: float64

If we only want to get the mean of one column, we can do this using the pandas mean() function in the following Python code:

print(df["Test_Score"].mean())

# Output:
88.0

Using numpy mean to Calculate Averages in pandas DataFrame

We can also use the numpy mean() function to calculate the mean value of the numbers in a column in a pandas DataFrame.

To get the average of the numbers in the column “Test_Score”, we can use the numpy mean() function in the following Python code:

print(np.mean(df["Test_Score"]))

# Output:
88.0

As you can see above, this is the same value we received from the pandas mean() function.

Hopefully this article has been helpful for you to understand how to find the mean value of numbers in a Series or DataFrame in pandas.

Other Articles You'll Also Like:

  • 1.  Python Get Number of Cores Using os cpu_count() Function
  • 2.  Flatten List of Tuples in Python
  • 3.  Remove Element from Set in Python
  • 4.  pandas round – Round Numbers in Series or DataFrame
  • 5.  Python Factorial Recursion – Using Recursive Function to Find Factorials
  • 6.  Golden Ratio Constant phi in Python
  • 7.  Python Split Tuple into Multiple Variables
  • 8.  Why Dictionaries Can’t Have Duplicate Keys in Python
  • 9.  Using Python to Sum Odd Numbers in List
  • 10.  How to Declare Variable Without Value in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy