如何在每组熊猫groupby对象中添加标志列

我有DF有三列XYZ我想将groupby功能应用于基于的分组数据X然后我想flag在每个组中插入一列。标志列的条件是,如果Column Z30%的值大于1.5,则为group添加标志列的值1。如果列Z30%的值不大于1.5,则为组添加标志列值0。

这是我的df示例:

 df = pd.DataFrame({'X':['1', '1', '1' ,'1', '1', '2','2','2','2','2','2','3','3','3'],'Y':["34","45","33","45","44", "66",'67','23','34','10','11','13','12','14'],'Z':["1.2","1.3","1.6","1.7","1.8", "0",'0','0','1.8','1.2','1.3','1.6','1.7','1.8']})
      X   Y    Z
0   1  34  1.2
1   1  45  1.3
2   1  33  1.6
3   1  45  1.7
4   1  44  1.8
5   2  66    0
6   2  67    0
7   2  23    0
8   2  34  1.8
9   2  10  1.2
10  2  11  1.3
11  3  13  1.6
12  3  12  1.7
13  3  14  1.8

预期结果:

df_result= pd.DataFrame({'X':['1', '1', '1' ,'1', '1', '2','2','2','2','2','2','3','3','3'],'Y':["34","45","33","45","44", "66",'67','23','34','10','11','13','12','14'],'Z':["1.2","1.3","1.6","1.7","1.8", "0",'0','0','1.8','1.2','1.3','1.6','1.7','1.8'],'flag':["1","1","1","1","1", "0",'0','0','0','0','0','1','1','1']})
print(df_result)
  X   Y    Z flag
0   1  34  1.2    1
1   1  45  1.3    1
2   1  33  1.6    1
3   1  45  1.7    1
4   1  44  1.8    1
5   2  66    0    0
6   2  67    0    0
7   2  23    0    0
8   2  34  1.8    0
9   2  10  1.2    0
10  2  11  1.3    0
11  3  13  1.6    1
12  3  12  1.7    1
13  3  14  1.8    1
耶斯列尔

GroupBy.transform与lambda函数一起使用,并通过Series.astype以下方式将布尔值转换为整数

df["Z"]= df["Z"].astype(float)

f = lambda x: (x > 1.5).sum() > len(x) *.3
#if necessary convert 30% to integer by ceil
#f = lambda x: (x > 1.5).sum() > np.ceil(len(x) *.3)
df['flag'] = df.groupby("X")["Z"].transform(f).astype(int)
print (df)
    X   Y    Z  flag
0   1  34  1.2     1
1   1  45  1.3     1
2   1  33  1.6     1
3   1  45  1.7     1
4   1  44  1.8     1
5   2  66  0.0     0
6   2  67  0.0     0
7   2  23  0.0     0
8   2  34  1.8     0
9   2  10  1.2     0
10  2  11  1.3     0
11  3  13  1.6     1
12  3  12  1.7     1
13  3  14  1.8     1

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章