我有一个熊猫数据框,如下所示:
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" type="text/css" href="css/maintravel2.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css" integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous">
<title>Round-a-Whirl</title>
</head>
<body>
<div class="wrapper">
<header>
<div></div>
<h2>HEADER LOGO</h2>
</header>
<div class="navbar">
<nav>
<ul>
<li><a href="#"><i class="fa fa-plane"> Flights</i></a></li>
<li><a href="#"><i class="fa fa-hotel"> Hotel</i></a></li>
<li><a href="#"><i class="fa fa-car"> Car Rental</i></a></li>
<li><a href="#"><i class="fa fa-globe"> Discover</i></a>
<ul>
<li>Vacation</li>
<li>Cruise</li>
<li>City</li>
<li>Nature</li>
<li>Getaways</li>
</ul>
</li>
<li><a href="#"><i class="fa fa-tag"> Deals</i></a></li>
</ul>
</nav>
</div>
<article>
This is a simple site I created using HTML and CSS of a travel site for the fictional Round-a-Whirl travel company.
</article>
<footer>
Round-a-Whirl © 2019
</footer>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
我想创建一个名为+-----+--------+-------+
| Age | PhysID | PedID |
+-----+--------+-------+
| 28 | 111 | 123 |
| 26 | 111 | 123 |
| 3 | 111 | 123 |
+-----+--------+-------+
的新列,如果DocID
的值等于PhysID
,否则的值等于Age>18
。输出如下:
PedID
是否有使用某些内置函数而不是编写自己的函数的干净方法?谢谢!
答案 0 :(得分:2)
使用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
答案 1 :(得分: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)
print(df)