以三组为单位分配值-Pandas DF

时间:2019-02-26 23:56:19

标签: python pandas numpy dataframe ceil

我正在尝试assign组中unique pandas的值df 3。对于下面的df,我有一个Column的值出现了很多次。我计算了这些值中有多少是当前发生的。也就是说,如果它们再次出现,则将它们视为 on

如果出现新值,它将增加打开的值的数量。如果没有再出现一个值,则会减少启用的值的数量。

这是我在下面的尝试。我可以把它分成三组。但是它不考虑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 4。应将Houses 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 

0 个答案:

没有答案