新列 - 来自多个行和列的多个条件
在带有三列的大数据框中,我必须根据前三列添加一个带有条件值的第四列。
这是关于旅行的。一个人(ID)有几种旅行选择(例如巴士,汽车,火车,...)。有时,一个人可以通过火车从A到B旅行几种选择。
我的数据框看起来像这样:
main_mode | 持续时间 | ID |
---|---|---|
火车 | 1250 | 1 |
火车 | 900 | 1 |
车 | 540 | 1 |
火车 | 650 | 2 |
汽车 | 450 | 2 |
平面 | 350 | 2 |
火车 | 350 | 3 |
CAR | 890 | 3 |
现在我想创建一个新的列“ Train_avail”。如果火车旅行的时间是汽车之旅的两倍,那么我想在新列中写下train_avail = 0,否则写1。
我想拥有的东西:
main_mode | distration | id | train_avail |
---|---|---|---|
train | 1250 | 1 | 0 |
火车 | 900 | 1 1 1 | 1 1 |
辆车 | 540 | 1 | NA |
火车 | 650 | 2 | 1 |
车 | 450 | 2 | NA |
平面 | 350 | 2 | NA |
火车 | 350 | 3 | 1 |
CAR | 890 | 3 | NA |
感谢您的帮助!
In a large dataframe with three columns I have to add a fourth column with values conditionally based on the first three columns.
It's about travel. A person (id) has several options to travel (e.g. bus, car, train, ...). Sometimes a person has several options by train to travel from A to B for example.
My dataframe looks like this:
main_mode | duration | id |
---|---|---|
train | 1250 | 1 |
train | 900 | 1 |
car | 540 | 1 |
train | 650 | 2 |
car | 450 | 2 |
plane | 350 | 2 |
train | 350 | 3 |
car | 890 | 3 |
Now I want to create a new column "train_avail". If the train journey takes twice as long as the car journey, then I would like to write train_avail = 0 in a new column otherwise write 1.
What i would like to have:
main_mode | duration | id | train_avail |
---|---|---|---|
train | 1250 | 1 | 0 |
train | 900 | 1 | 1 |
car | 540 | 1 | NA |
train | 650 | 2 | 1 |
car | 450 | 2 | NA |
plane | 350 | 2 | NA |
train | 350 | 3 | 1 |
car | 890 | 3 | NA |
Thanks for your help!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)