根据熊猫数据框中其他列的条件和值创建新列[重复]
Posted
技术标签:
【中文标题】根据熊猫数据框中其他列的条件和值创建新列[重复]【英文标题】: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 |
+-----+--------+-------+-------+
有没有一种干净的方法可以使用一些内置函数而不是自己编写函数?谢谢!
【问题讨论】:
【参考方案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)
【讨论】:
以上是关于根据熊猫数据框中其他列的条件和值创建新列[重复]的主要内容,如果未能解决你的问题,请参考以下文章