Panda DataFrame.mean()
La funzione mean() viene utilizzata per restituire la media dei valori per l'asse richiesto. Se applichiamo questo metodo su a Oggetto di serie , quindi restituisce a valore scalare , che è il valore medio di tutte le osservazioni nel dataframe.
Se applichiamo questo metodo su un oggetto DataFrame, restituisce un oggetto Series che contiene la media dei valori sull'asse specificato.
Sintassi
DataFrame.mean(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)
Parametri
Si riferisce all'asse di una funzione da applicare.
ritorna
Restituisce la media della serie o del DataFrame se il livello è specificato.
Esempio
# 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) Produzione
A 7.0 B 13.2 C 8.6 D 15.4 dtype: float64
Esempio2
# 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) Produzione
0 11.500000 1 16.000000 2 15.333333 3 9.333333 4 15.666667 dtype: float64