Python|熊猫 dataframe.quantile()
Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas就是其中之一,它使导入和分析数据变得更加容易。
Pandas dataframe.quantile()
函数返回请求轴上给定分位数的值,一个 numpy.percentile。
注意:在变量的任何一组值中,将频率分布分成相等的组,每个组包含总人口的相同比例。
Syntax: DataFrame.quantile(q=0.5, axis=0, numeric_only=True, interpolation=’linear’)
Parameters :
q : float or array-like, default 0.5 (50% quantile). 0 <= q <= 1, the quantile(s) to compute
axis : [{0, 1, ‘index’, ‘columns’} (default 0)] 0 or ‘index’ for row-wise, 1 or ‘columns’ for column-wise
numeric_only : If False, the quantile of datetime and timedelta data will be computed as well
interpolatoin : {‘linear’, ‘lower’, ‘higher’, ‘midpoint’, ‘nearest’}
Returns : quantiles : Series or DataFrame
-> If q is an array, a DataFrame will be returned where the index is q, the columns are the columns of self, and the values are the quantiles.
-> If q is a float, a Series will be returned where the index is the columns of self and the values are the quantiles.
示例 #1:使用quantile()
函数查找“.2”分位数的值
# importing pandas as pd
import pandas as pd
# Creating the dataframe
df = pd.DataFrame({"A":[1, 5, 3, 4, 2],
"B":[3, 2, 4, 3, 4],
"C":[2, 2, 7, 3, 4],
"D":[4, 3, 6, 12, 7]})
# Print the dataframe
df
让我们使用dataframe.quantile()
函数来查找数据帧中每一列的 '.2' 的分位数
# find the product over the index axis
df.quantile(.2, axis = 0)
输出 :
示例 #2:使用quantile()
函数沿索引轴查找 (.1, .25, .5, .75) 分位数。
# importing pandas as pd
import pandas as pd
# Creating the dataframe
df = pd.DataFrame({"A":[1, 5, 3, 4, 2],
"B":[3, 2, 4, 3, 4],
"C":[2, 2, 7, 3, 4],
"D":[4, 3, 6, 12, 7]})
# using quantile() function to
# find the quantiles over the index axis
df.quantile([.1, .25, .5, .75], axis = 0)
输出 :