【问题标题】:Creating new column based on conditions and values from other columns in a pandas dataframe [duplicate]根据熊猫数据框中其他列的条件和值创建新列[重复]
【发布时间】:2019-07-25 06:04:39
【问题描述】:

我有一个看起来像这样的 pandas 数据框:

+-----+--------+-------+
| Age | PhysID | PedID |
+-----+--------+-------+
|  28 |    111 |   123 |
|  26 |    111 |   123 |
|   3 |    111 |   123 |
+-----+--------+-------+

我想创建一个名为DocID 的新列,如果Age>18 的值等于PhysID,否则等于PedID。输出如下所示:

+-----+--------+-------+-------+
| Age | PhysID | PedID | DocID |
+-----+--------+-------+-------+
|  28 |    111 |   123 |   111 |
|  26 |    111 |   123 |   111 |
|   3 |    111 |   123 |   123 |
+-----+--------+-------+-------+

有没有一种干净的方法可以使用一些内置函数而不是自己编写函数?谢谢!

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    使用np.where

    df['NewId']=np.where(df.Age>18,df.PhysID,df.PedID)
    df
       Age  PhysID  PedID  NewId
    0   28     111    123    111
    1   26     111    123    111
    2    3     111    123    123
    

    【讨论】:

      【解决方案2】:

      lambda 函数很适合这类问题

      df = pd.DataFrame({'Age':[28,26,3],'PhysID':[111,111,111],'PedID':[123,123,123]})
      
      df['DocId'] = df.apply(lambda x: x['PhysID'] if x['Age'] > 18 else x['PedID'], axis=1)
      

      打印(df)

      【讨论】:

        猜你喜欢
        • 2022-12-08
        • 2021-12-24
        • 1970-01-01
        • 1970-01-01
        • 2022-06-10
        • 2018-11-06
        • 2021-08-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多