如何在pandas groupby中移动整个组 [英] How to shift entire groups in pandas groupby

查看:56
本文介绍了如何在pandas groupby中移动整个组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定以下数据:

data = {'a' : [1,1,1,8,8,3,3,3,3,4,4] }df = pd.DataFrame(data)

我现在想将整个事物向下移动 n ,以便保留它们当前的顺序.移位 n=1 所需的输出为:

desired_output = {'a': [NaN,NaN,NaN,1,1,8,8,8,8,3,3] }required_output_df = pd.DataFrame(desired_output)

n=2 的移位应该是:

desired_output = {'a': [NaN,NaN,NaN,NaN,NaN,1,1,1,1,8,8] }required_output_df = pd.DataFrame(desired_output)

我一直在忙于 groupby/transform/apply,但到目前为止还没有任何工作.如果我分组然后移动,它会移动每个组,给出以下输出:

NOT_desired_output = {'a' : [NaN, 1, 1, NaN, 8, NaN, 3,3,3, NaN, 4]}

我可以通过迭代来暴力破解它,但我相信有更好的解决方案.有什么想法吗?

解决方案

这是一个有趣的操作.我可以想出一种替代方法来使用 replace.

按 1 组转移:

<预><代码>>>>df['b'] = df.a.shift()>>>x = df[df.a != df.b]>>>df.replace(*x.values.T)

提供数据帧:

 a b0 南南1 南南2 南南3 1 南4 1 15 8 16 8 87 8 88 8 89 3 810 3 3

我们只想要这个 DataFrame 的 a 列:

desired_output_df = pd.DataFrame(_, columns=['a'])

要移动多组,只需移动xb列.如果要按n 组移动,则需要将x.b 再移动n-1 次.只需插入行

<预><代码>>>>x.b = x.b.shift(n-1)

x = df[df.a != df.b] 之后,然后执行 df.replace(*x.values.T) 步骤.>

Given the following data:

data = {'a' : [1,1,1,8,8,3,3,3,3,4,4] }
df = pd.DataFrame(data)

I would now like to shift the whole thing down by n groups, so that their current order is preserved. The desired output for a shift of n=1 would be:

desired_output = {'a': [NaN,NaN,NaN,1,1,8,8,8,8,3,3] }
desired_output_df = pd.DataFrame(desired_output)

a shift of n=2 should be:

desired_output = {'a': [NaN,NaN,NaN,NaN,NaN,1,1,1,1,8,8] }
desired_output_df = pd.DataFrame(desired_output)

I have been messing around with groupby/transform/apply but haven't gotten anything to work so far. If I groupby and then shift, it shifts each group giving the output of:

NOT_desired_output = {'a' : [NaN, 1, 1, NaN, 8, NaN, 3,3,3, NaN, 4]}

I could brute force it by iterating, but I'm sure there's a better solution. Any ideas?

解决方案

This is an interesting operation. I can think of an alternative way to do it with replace.

To shift by 1 group:

>>> df['b'] = df.a.shift()
>>> x = df[df.a != df.b]
>>> df.replace(*x.values.T)

Which gives the DataFrame:

     a   b
0  NaN NaN
1  NaN NaN
2  NaN NaN
3    1 NaN
4    1   1
5    8   1
6    8   8
7    8   8
8    8   8
9    3   8
10   3   3

And we just want column a of this DataFrame:

desired_output_df = pd.DataFrame(_, columns=['a'])

To shift by more than one group, you just need to shift column b of x. If you want to shift by n groups, you need to shift x.b an additional n-1 times. Just insert the line

>>> x.b = x.b.shift(n-1)

after x = df[df.a != df.b] and then perform the df.replace(*x.values.T) step.

这篇关于如何在pandas groupby中移动整个组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆