【问题标题】:Pandas delete and shift cells in a column basis multiple conditionsPandas 在多个条件下按列删除和移动单元格
【发布时间】:2020-08-18 19:39:59
【问题描述】:

我有一种情况,我想根据某些条件删除和移动 pandas 数据框中的单元格。我的数据框如下所示:

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

【问题讨论】:

  • 一个空字符串不是一个空的道歉..
  • 在原始数据中,列不是那样的..就像 _ 后面没有数字一样。为简单起见,您可以将列名视为 A、B、C、D
  • 是的,只有 6 列,可以使用名称进行选择。将在三列之间进行比较,如果 col ID_2 和 ID_3 中的值小于或等于 ID_1,那么它会保持不变,否则会像我展示的那样移动

标签: python-3.x pandas merge shift del


【解决方案1】:

您可以按条件创建掩码,此处为更大的值,例如 ID_1 by 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替换缺失值:

df[cols2] = df[cols2].mask(m) 
df[cols1] = df[cols1].mask(m.to_numpy()) 

最后使用DataFrame.shiftSeries.mask 设置新列:

df1 = df[cols2].shift(-1, axis=1)
df['ID_2'] =  df['ID_2'].mask(m['ID_2'], df1['ID_2'])
df['ID_3'] =  df['ID_3'].mask(m['ID_2'])

df2 = df[cols1].shift(-1, axis=1)
df['Value_2'] =  df['Value_2'].mask(m['ID_2'], df2['Value_2'])
df['Value_3'] =  df['Value_3'].mask(m['ID_2'])

print (df)
  Value_1  ID_1 Value_2  ID_2 Value_3  ID_3
0       A     1       D   1.0       G   1.0
1       B     1       H   1.0     NaN   NaN
2       C     1     NaN   NaN     NaN   NaN
3       C     1       H   1.0     NaN   NaN

如果需要,最后用空字符串替换:

df[cols1] = df[cols1].fillna('')
print (df)
  Value_1  ID_1 Value_2  ID_2 Value_3  ID_3
0       A     1       D   1.0       G   1.0
1       B     1       H   1.0           NaN
2       C     1           NaN           NaN
3       C     1       H   1.0           NaN

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 1970-01-01
    • 2019-01-16
    • 2021-05-21
    • 2020-02-09
    • 1970-01-01
    相关资源
    最近更新 更多