三人一组赋值

2024-04-23 08:12:37 发布

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

我正在尝试将assignunique值从pandasdf分组3。对于下面的df,我有一个Column的值,它出现了很多次。我计算这些值中有多少是当前发生的。也就是说,如果它们再次出现,则被认为是上。你知道吗

如果出现新值,则会增加启用的值数。如果某个值不再出现,则会减少打开的值的数量。

下面是我的尝试。我可以把它分成三组,但它不考虑unique值。你知道吗

import pandas as pd
import numpy as np

d = ({             
   'Place' : ['House 1','House 2','House 3','House 4','House 1','House 2','House 3','House 4'],#,'House 1','House 2']#,'House 4','House 5','House 6','House 7'],                                                                        
   'On' : [1,2,3,4,4,3,2,1],                                                               
     })

df = pd.DataFrame(data=d)

df["Ind"] = np.ceil(df["On"]/3)

Output:

     Place  On    P
0  House 1   1  1.0
1  House 2   2  1.0
2  House 3   3  1.0
3  House 4   4  2.0
4  House 1   4  2.0
5  House 2   3  1.0
6  House 3   2  1.0
7  House 4   1  1.0

预期输出:

     Place  On    P
0  House 1   1  1.0
1  House 2   2  1.0
2  House 3   3  1.0
3  House 4   4  2.0
4  House 1   4  1.0
5  House 2   3  1.0
6  House 3   2  1.0
7  House 4   1  1.0

说明:

区别在于Index 4Houses 1应该分配给1,因为它们最初分配给这个integer。你知道吗

On列的说明:

Index 0: House 1 is inserted and it appears again = 1 
Index 1: House 2 is inserted and it appears again = 2 
Index 2: House 3 is inserted and it appears again = 3 
Index 3: House 4 is inserted and it appears again = 4 
Index 4: House 1 doesn't appear again so it will decrease number of values on on the next row = 4 
Index 5: House 2 doesn't appear again so it will decrease number of values on  = 3 
Index 6: House 3 doesn't appear again so it will decrease number of values on  = 2 
Index 7: House 4 doesn't appear again so it will decrease number of values on  = 1 

Tags: anddfindexsoisonitwill