pandas 熊猫 groupby 和 qcut
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19403133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas groupby and qcut
提问by mhabiger
Is there a way to structure Pandas groupby and qcut commands to return one column that has nested tiles? Specifically, suppose I have 2 groups of data and I want qcut applied to each group and then return the output to one column. This would be similar to MS SQL Server's ntile() command that allows Partition by().
有没有办法构造 Pandas groupby 和 qcut 命令以返回具有嵌套图块的列?具体来说,假设我有 2 组数据,并且我希望将 qcut 应用于每组,然后将输出返回到一列。这类似于 MS SQL Server 的 ntile() 命令,它允许 Partition by()。
A B C
0 foo 0.1 1
1 foo 0.5 2
2 foo 1.0 3
3 bar 0.1 1
4 bar 0.5 2
5 bar 1.0 3
In the dataframe above I would like to apply the qcut function to B while partitioning on A to return C.
在上面的数据框中,我想将 qcut 函数应用于 B,同时在 A 上进行分区以返回 C。
回答by unutbu
import pandas as pd
df = pd.DataFrame({'A':'foo foo foo bar bar bar'.split(),
'B':[0.1, 0.5, 1.0]*2})
df['C'] = df.groupby(['A'])['B'].transform(
lambda x: pd.qcut(x, 3, labels=range(1,4)))
print(df)
yields
产量
A B C
0 foo 0.1 1
1 foo 0.5 2
2 foo 1.0 3
3 bar 0.1 1
4 bar 0.5 2
5 bar 1.0 3

