【问题标题】:Python dataframe count the values in a row and add status to the another rowPython数据框计算一行中的值并将状态添加到另一行
【发布时间】:2021-03-02 08:47:21
【问题描述】:

有一个带有值的df

name    subject  

mark     M         
mark     S
mark     P
mark     SS
staurt   M
cuban    S
cuban    P

如果名称中的用户数在名称列中多次出现,则当​​前状态应为 YES ,如果仅出现一次,则状态当前状态应为 NO

name    subject   present status

mark     M          YES
mark     S          YES
mark     P          YES
mark     SS         YES
staurt   M          No
cuban    S          YES
cuban    P          YES

试过这个:

df['当前状态'] = np.where(df['name'] == df['name'].shift(), 'YES', 'NO')

【问题讨论】:

    标签: python python-3.x pandas dataframe numpy


    【解决方案1】:

    Series.duplicatedkeep=False 一起使用:

    df['present status'] = np.where(df['name'].duplicated(keep=False), 'YES', 'NO')
    print (df)
         name subject present status
    0    mark       M            YES
    1    mark       S            YES
    2    mark       P            YES
    3    mark      SS            YES
    4  staurt       M             NO
    5   cuban       S            YES
    6   cuban       P            YES
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-03
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-17
      • 1970-01-01
      相关资源
      最近更新 更多