Pandas DataFrame: Calculate the mean of all students' scores
14. Calculating the Mean of Scores
Write a Pandas program to calculate the mean of all students' scores. Data is stored in a dataframe.
Sample DataFrame:
exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
Sample Solution :
Python Code :
import pandas as pd
import numpy as np
exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
df = pd.DataFrame(exam_data , index=labels)
print("\nMean score for each different student in data frame:")
print(df['score'].mean())
Sample Output:
Mean score for each different student in data frame: 13.5625
Explanation:
The above code first creates a Pandas DataFrame 'df' from a dictionary 'exam_data' with row labels 'labels'. It then computes and prints the mean value of the 'score' column of the DataFrame using the mean() method of Pandas.
The 'score' column contains numerical values with one missing value represented by numpy.nan. The mean() method automatically excludes the missing value from the computation.
For more Practice: Solve these Related Problems:
- Write a Pandas program to compute the mean of the 'score' column, ignoring NaN values, and then round the result to two decimals.
- Write a Pandas program to calculate the mean score after removing the highest and lowest values from the DataFrame.
- Write a Pandas program to compute the mean of scores for a subset of the DataFrame where 'attempts' is less than a given value.
- Write a Pandas program to calculate the mean score and then add this value as a new column to every row in the DataFrame.
Python-Pandas Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Pandas program to calculate the sum of the examination attempts by the students.
Next: Write a Pandas program to append a new row 'k' to DataFrame with given values for each column. Now delete the new row and return the original data frame.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.