How to get the mean of a specific column in a dataframe in Python?

PythonServer Side ProgrammingProgramming

Sometimes, it may be required to get the mean value of a specific column that is numeric in nature. This is where the ‘mean’ function can be used.

The column whose mean needs to be computed can be indexed to the dataframe, and the mean function can be called on this using the dot operator.

The index of the column can also be passed to find the mean. 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

 Live Demo

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 of column 'Age' is :")
print(my_df['Age'].mean())
print("The mean of column 'value' is :")
print(my_df['value'].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 of column 'Age' is :
47.2
The mean of column 'value' is :
46.553999999999995

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 a specific column that contain numeric values in them.

  • The ‘mean’ function is called on the dataframe by specifying the name of the column, using the dot operator.

  • The mean of numeric column is printed on the console.

raja
Updated on 10-Dec-2020 13:23:58

Advertisements