Python是進行數據分析的一種出色語言,主要是因為以數據為中心的python軟件包具有奇妙的生態係統。 Pandas是其中的一種,使導入和分析數據更加容易。
Pandas dataframe.median()
函數返回所請求軸的值的中值
如果將方法應用於 Pandas 係列對象,則該方法將返回標量值,該標量值是 DataFrame 中所有觀測值的中值。如果將方法應用於pandas DataFrame 對象,則該方法將返回pandas係列對象,該對象包含指定軸上的值的中值。
用法:DataFrame.median(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)
參數:
axis:Align object with threshold along the given axis.
skipna:Exclude NA/null values when computing the result
level:If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series
numeric_only:Include only float, int, boolean columns. If None, will attempt to use everything, then use only numeric data. Not implemented for Series.
返回值:中位數:Series或DataFrame(如果指定級別)
範例1:采用median()
函數查找索引軸上所有觀測值的中位數。
# importing pandas as pd
import pandas as pd
# Creating the dataframe
df = pd.DataFrame({"A":[12, 4, 5, 44, 1],
"B":[5, 2, 54, 3, 2],
"C":[20, 16, 7, 3, 8],
"D":[14, 3, 17, 2, 6]})
# Print the dataframe
df
讓我們使用dataframe.median()
查找索引軸上的中位數的函數
# Find median Even if we do not specify axis = 0, the method
# will return the median over the index axis by default
df.median(axis = 0)
輸出:
範例2:采用median()
在具有Na
值。還要找到列軸的中位數。
# importing pandas as pd
import pandas as pd
# Creating the dataframe
df = pd.DataFrame({"A":[12, 4, 5, None, 1],
"B":[7, 2, 54, 3, None],
"C":[20, 16, 11, 3, 8],
"D":[14, 3, None, 2, 6]})
# Print the dataframe
df
讓我們實現中位數函數。
# skip the Na values while finding the median
df.median(axis = 1, skipna = True)
輸出:
注:本文由純淨天空篩選整理自 Python | Pandas dataframe.median()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。