TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Pandas DataFrame.mean()

Pandas DataFrame.mean() with What is Python Pandas, Reading Multiple Files, Null values, Multiple index, Application, Application Basics, Resampling, Plotting the data, Moving windows functions, Series, Read the file, Data operations, Filter Data etc.

<< Back to PANDAS

Pandas DataFrame.mean()

The mean() function is used to return the mean of the values for the requested axis. If we apply this method on a Series object, then it returns a scalar value, which is the mean value of all the observations in the dataframe.

If we apply this method on a DataFrame object, then it returns a Series object which contains mean of values over the specified axis.

Syntax

DataFrame.mean(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)

Parameters

  • axis: {index (0), columns (1)}.
    This refers to the axis for a function that is to be applied.
  • skipna: It excludes all the null values when computing result.
  • level: It counts along with a particular level and collapsing into a Series if the axis is a MultiIndex (hierarchical),
  • numeric_only: It includes only int, float, boolean columns. If None, it will attempt to use everything, then use only numeric data. Not implemented for Series.

Returns

It returns the mean of the Series or DataFrame if the level is specified.

Example

# importing pandas as pd 
import pandas as pd  
# Creating the dataframe  
info = pd.DataFrame({"A":[8, 2, 7, 12, 6], 
                   "B":[26, 19, 7, 5, 9],  
                   "C":[10, 11, 15, 4, 3], 
                   "D":[16, 24, 14, 22, 1]})   
# Print the dataframe 
info
# If axis = 0 is not specified, then
# by default method return the mean over 
# the index axis 
info.mean(axis = 0)

Output

A     7.0
B    13.2
C     8.6
D    15.4
dtype: float64

Example2

# importing pandas as pd 
import pandas as pd 
# Creating the dataframe  
info = pd.DataFrame({"A":[5, 2, 6, 4, None], 
                   "B":[12, 19, None, 8, 21], 
                   "C":[15, 26, 11, None, 3],
                   "D":[14, 17, 29, 16, 23]})   
# while finding mean, it skip null values 
info.mean(axis = 1, skipna = True) 

Output

0       11.500000
1       16.000000
2       15.333333
3        9.333333
4       15.666667
dtype: float64

Next TopicDataFrame.melt()




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf