如何在 Pandas 中按组计算分位数?
在本文中,如何使用Python在 Pandas 中按组计算分位数。
计算分位数的方法有很多,但 pandas 提供了 groupby.quantile()函数,只需几行代码即可找到它。这是当所需分位数落在两点之间时使用的方法。
Syntax:
DataFrameGroupBy.quantile(self, q=0.5, interpolation=’linear’)
Parameters:
- q : float or array-like, default 0.5 (50% quantile) Values are given between 0 and 1 providing the quantiles to compute.
- Interpolation : {‘linear’, ‘lower’, ‘higher’, ‘midpoint’, ‘nearest’}
在此方法中,值和插值作为参数传递。默认情况下,q 值为 0.5,插值将是线性的。这将返回由 GroupBy 对象确定的系列或数据框。
正在使用的数据框:
示例 1:按组计算分位数
Python3
# Importing libraries
import pandas as pd
# Storing data in dictionary
game = {'Player': ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B',
'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C'],
'wins': [2, 4, 4, 5, 6, 9, 13, 13, 15, 15, 14, 13,
11, 9, 9, 8, 8, 16, 19, 21, 14, 20, 19, 18]
}
# Creating data frame
df = pd.DataFrame(game)
# calculating quantile
df.groupby('Player').quantile(0.5)
Python3
# Importing libraries
import pandas as pd
# Storing data in dictionary
game = {'Player': ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B',
'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C'],
'wins': [2, 4, 4, 5, 6, 9, 13, 13, 15, 15, 14, 13,
11, 9, 9, 8, 8, 16, 19, 21, 14, 20, 19, 18]
}
# Creating data frame
df = pd.DataFrame(game)
# calculating quantile
df.groupby('Player').quantile(0.9)
输出:
示例 2:按组计算分位数
Python3
# Importing libraries
import pandas as pd
# Storing data in dictionary
game = {'Player': ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B',
'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C'],
'wins': [2, 4, 4, 5, 6, 9, 13, 13, 15, 15, 14, 13,
11, 9, 9, 8, 8, 16, 19, 21, 14, 20, 19, 18]
}
# Creating data frame
df = pd.DataFrame(game)
# calculating quantile
df.groupby('Player').quantile(0.9)
输出: