简体   繁体   English

熊猫groupby和qcut

[英]Pandas groupby and qcut

Is there a way to structure Pandas groupby and qcut commands to return one column that has nested tiles? 有没有办法构建Pandas groupby和qcut命令返回一个具有嵌套切片的列? Specifically, suppose I have 2 groups of data and I want qcut applied to each group and then return the output to one column. 具体来说,假设我有2组数据,我希望qcut应用于每个组,然后将输出返回到一列。 This would be similar to MS SQL Server's ntile() command that allows Partition by(). 这类似于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.

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM