熊猫在多个条件下删除和移动列中的单元格

问题描述 投票:1回答:1

我有一种情况,在某些情况下,我想删除并移动熊猫数据框中的单元格。我的数据框看起来像这样:

Value_1      ID_1      Value_2      ID_2         Value_3      ID_3
   A           1            D         1               G          1
   B           1            E         2               H          1
   C           1            F         2               I          3
   C           1            F         2               H          1

现在我想比较以下条件:

ID_2 and ID_3 should always be less than or equal to ID_1. If anyone of them is greater than ID_1 then that cell should be deleted and shifted with the next column cell

输出应如下所示:

    Value_1      ID_1      Value_2      ID_2         Value_3      ID_3
       A           1            D         1               G          1
       B           1            H         1           blank        nan
       C           1       blank        nan           blank        nan
       C           1            H         1           blank        nan
python-3.x pandas merge shift del
1个回答
0
投票

您可以按条件创建遮罩,此处可使用ID_1DataFrame.gt ::]等更大的值>

DataFrame.gt

如果匹配掩码为cols1 = ['Value_2','Value_3'] cols2 = ['ID_2','ID_3'] m = df[cols2].gt(df['ID_1'], axis=0) print (m) ID_2 ID_3 0 False False 1 True False 2 True True 3 True False ,则替换缺失的值:

DataFrame.mask

最后使用DataFrame.mask,并按df[cols2] = df[cols2].mask(m) df[cols1] = df[cols1].mask(m.to_numpy()) 设置新列:

DataFrame.shift
© www.soinside.com 2019 - 2024. All rights reserved.