【问题标题】:applying conditions basis the value in column to create new tag应用条件根据列中的值创建新标签
【发布时间】:2022-11-21 15:30:49
【问题描述】:

现有数据框:

Id     created_by
A        A
A        123
B        X
B        B

预期数据框:

Id     created_by    status
A        A           category_1
A        123         category_2
B        X           category_3
B        B           category_1

我希望根据条件创建状态标签:

if Id == created_by  >> category_1

if id != created_by  >> category_2

if id != created_by & created_by == 'X'  >> category_3

我正在使用以下代码:

conditions = [
              df['Id'] == df['created_by'], 
              df['Id'] != df['created_by'],
              (df['Id'] != df['created_by']) & (df['created_by'] == 'X')

             ]

# Creating Labels
result = ['category_1','category_2','category_3']

# Creating status column
df['status'] = np.select(conditions, result , default='REST')

不知何故,我没有得到第三种情况的正确数字。我错过了什么

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    问题出在第二种情况,有必要在created_by中添加过滤非X值:

    conditions = [
                  df['Id'] == df['created_by'], 
                  (df['Id'] != df['created_by']) & (df['created_by'] != 'X'),
                  (df['Id'] != df['created_by']) & (df['created_by'] == 'X')
    
                 ]
    
    # Creating Labels
    result = ['category_1','category_2','category_3']
    
    # Creating status column
    df['status'] = np.select(conditions, result , default='REST')
    print (df)
      Id created_by      status
    0  A          A  category_1
    1  A        123  category_2
    2  B          X  category_3
    3  B          B  category_1
    

    【讨论】:

      猜你喜欢
      • 2022-01-05
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      • 2020-10-02
      • 2022-01-07
      • 1970-01-01
      • 2016-10-07
      相关资源
      最近更新 更多