查找重复值包并分别对其执行操作

2024-05-04 21:21:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这种类型的熊猫系列

    a = pd.Series([1,  4,3,5,  7,5,  5,6,7,6,  7,  6,2,2,  6,  9])

和类型的numpy数组

    b = np.array([0,  1,1,1,  0,0,  1,1,1,1,  0,  1,1,1,  0,  1])

现在我想分别生成a中元素的平均值,只要在b的相应部分中有一个1的集群,结果是

    c = pd.Series([1,  4,4,4,  7,5,  6,6,6,6,  7,  3.3,3.3,3.3  6,  9])

有人知道如何很好地做到这一点吗


Tags: numpy元素类型np集群数组array平均值
2条回答

方法#1

这里有一个简单的方法-

In [23]: ids = np.r_[0,b[:-1]!=b[1:]].cumsum()

In [24]: np.where(b==1,a.groupby(ids).transform('mean'),a)
Out[24]: 
array([1.        , 4.        , 4.        , 4.        , 7.        ,
       5.        , 6.        , 6.        , 6.        , 6.        ,
       7.        , 3.33333333, 3.33333333, 3.33333333, 6.        ,
       9.        ])

方法#2

对于性能,我们可以利用^{}-

In [47]: v = np.bincount(ids,a)/np.bincount(ids)

In [48]: np.where(b==1,v[ids],a)
Out[48]: 
array([1.        , 4.        , 4.        , 4.        , 7.        ,
       5.        , 6.        , 6.        , 6.        , 6.        ,
       7.        , 3.33333333, 3.33333333, 3.33333333, 6.        ,
       9.        ])

尝试使用shift+cumsum,注意6,2,2,的平均值是3.333..而不是5

s = pd.Series(b,index=a.index)
a.groupby(s.ne(s.shift()).cumsum()).transform('mean').where(s.eq(1),a)

0     1.000000
1     4.000000
2     4.000000
3     4.000000
4     7.000000
5     5.000000
6     6.000000
7     6.000000
8     6.000000
9     6.000000
10    7.000000
11    3.333333
12    3.333333
13    3.333333
14    6.000000
15    9.000000
dtype: float64

相关问题 更多 >