Python Pandas program to calculate the mean of a column

In this python pandas program, we will calculate the mean of a column using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Calculate the mean of the age column from the DataFrame using df[‘Age’].mean().
  4. Print the output.
				
					import pandas as pd
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33]}
df = pd.DataFrame(d)
print(df)
print("Sum of age columns: ",df['Age'].mean())
				
			

Output :

				
					0   Sr.no.   Name  Age
0       1   Alex   30
1       2   John   27
2       3  Peter   29
3       4  Klaus   33
Sum of age columns:  29.75
				
			

sort the DataFrame first by ‘name’ in ascending order

sort the DataFrame by ‘age’ column in ascending order

Leave a Comment