简体   繁体   English

Pandas Groupby 按递减顺序替换值

[英]Pandas Groupby replace values in decremental order

how can i replace the values in a column in a decremental order with maximum value of the second column is retained and other values are decremented by one from this value for a particular group in pandas?如何以递减顺序替换列中的值,保留第二列的最大值,而其他值从该值中为熊猫中的特定组递减一个?

I have a dataframe with 2 columns A and B我有一个包含 2 列 A 和 B 的数据框

Input :输入 :

AB AB

210 2 210 2
210 1 210 1
210 5 210 5
210 3 210 3
145 1 145 1
145 3 145 3
145 3 145 3
145 6 145 6

desired output:所需的输出:


AB AB

210 2 210 2
210 3 210 3
210 4 210 4
210 5 210 5
145 3 145 3
145 4 145 4
145 5 145 5
145 6 145 6

Use groupby.cumcount and then you can add the difference between the maximum and the group size using groupby.transform :使用groupby.cumcount然后你可以添加的最大和组大小两者的区别groupby.transform

groups = df.groupby('A').B
df['B']=( groups.cumcount()
                .add(1)
                .add(groups.transform('max')
                           .sub(groups.transform('size')) )
        )
print(df)

Output输出

     A  B
0  210  2
1  210  3
2  210  4
3  210  5
4  145  3
5  145  4
6  145  5
7  145  6

Time comparision时间比较

%%timeit
groups = df.groupby('A').B
df['B']=( groups.cumcount()
                .add(1)
                .add(groups.transform('max')
                           .sub(groups.transform('size')))
        )
#3.33 ms ± 66 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%%timeit
def custom_f(grp):
  m = grp.max()
  return np.arange(m - grp.shape[0]+1 , m+1)
df['B'] = df[['A','B']].groupby('A').transform(custom_f)
#9.18 ms ± 890 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

You can do the following.您可以执行以下操作。 Basically, we are creating a range for each group and the range goes from max - num_rows + 1 to m .基本上,我们为每个组创建一个范围,范围从max - num_rows + 1m

def custom_f(grp):
  m = grp.max()
  return np.arange(m - grp.shape[0]+1 , m+1)
df['B'] = df[['A','B']].groupby('A').transform(custom_f)

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

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