

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get the mean of columns that contains numeric values of a dataframe in Pandas Python?
Sometimes, it may be required to get the mean values of a specific column or mean values of all columns that contains numerical values. This is where the mean() function can be used.
The term ‘mean’ refers to finding the sum of all values and dividing it by the total number of values in the dataset.
Let us see a demonstration of the same −
Example
import pandas as pd my_data = {'Name':pd.Series(['Tom','Jane','Vin','Eve','Will']), 'Age':pd.Series([45, 67, 89, 12, 23]), 'value':pd.Series([8.79,23.24,31.98,78.56,90.20]) } print("The dataframe is :") my_df = pd.DataFrame(my_data) print(my_df) print("The mean is :") print(my_df.mean())
Output
The dataframe is : Name Age value 0 Tom 45 8.79 1 Jane 67 23.24 2 Vin 89 31.98 3 Eve 12 78.56 4 Will 23 90.20 The mean is : Age 47.200 value 46.554 dtype: float64
Explanation
The required libraries are imported, and given alias names for ease of use.
Dictionary of series consisting of key and value is created, wherein a value is actually a series data structure.
This dictionary is later passed as a parameter to the ‘Dataframe’ function present in the ‘pandas’ library
The dataframe is printed on the console.
We are looking at computing the mean of all columns that contain numeric values in them.
The ‘mean’ function is called on the dataframe using the dot operator.
The mean of numeric columns is printed on the console.
- Related Questions & Answers
- Python - Calculate the mean of column values of a Pandas DataFrame
- Python Pandas - Query the columns of a DataFrame
- Python - Renaming the columns of Pandas DataFrame
- Correlation between two numeric columns in a Pandas DataFrame
- How to get the mean of a specific column in a dataframe in Python?
- How to change the order of Pandas DataFrame columns?
- How to sort multiple columns of a Pandas DataFrame?
- Python - Replace values of a DataFrame with the value of another DataFrame in Pandas
- Python - Calculate the median of column values of a Pandas DataFrame
- Python - Calculate the count of column values of a Pandas DataFrame
- Python - Calculate the maximum of column values of a Pandas DataFrame
- Python - Calculate the minimum of column values of a Pandas DataFrame
- How to get the sum of a specific column of a dataframe in Pandas Python?
- Python Pandas – Get the datatype and DataFrame columns information
- How to get the row count of a Pandas DataFrame?