【问题标题】:Adding a new column for a certain condition for a combination of two column values in dataframe为数据框中两个列值的组合添加特定条件的新列
【发布时间】:2019-09-22 16:36:46
【问题描述】:

我在表中有 3 列,最后一列为 "Status" 。我希望对表进行分组并添加一个新列 "Final Status" 。如果对于任何 dept_id 和 session_id 组合,最终状态应为“失败”,原始表中的状态为失败。

我的数据和预期结果如下所示:

Dept_id   Session_id   Status
a         S1           Pass
a         S1           Pass 
a         S1           Fail
b         S1           Pass
b         S1           Pass
c         S1           Fail
a         S2           Pass
a         S2           Pass

预期结果:

Dept_id    Session_id    Final_Status
a          S1            Fail
b          S1            Pass 
c          S1            Fail
a          S2            Pass

说明 - 由于组合 "a" 和 "S1" 的状态为 "Fail",因此最终状态为失败,而组合 "b" 和 "S1" 的状态为失败,因此 Final_Status 为通过.

【问题讨论】:

    标签: python python-3.x pandas python-2.7 pandas-groupby


    【解决方案1】:

    你也可以试试这个答案:

    df.replace({'Pass':True,'Fail':False}).groupby(['Dept_id', 'Session_id']).all().reset_index().replace({'True':'Pass','False':'Fail'})

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-15
      • 2014-06-20
      • 2022-01-18
      • 2022-11-25
      • 1970-01-01
      • 2021-12-28
      相关资源
      最近更新 更多