简体   繁体   English

具有多个条件的 df.loc

[英]df.loc with more than one condition

I am trying to produce a variable in my dataframe that states if the specfied time is in rush hour using two other variables 'hour' and 'weekday?'.When I run the code it only states 'NaN', can someone guide me on what is wrong with my code?我正在尝试在我的 dataframe 中生成一个变量,该变量使用另外两个变量“小时”和“工作日”来说明指定时间是否处于高峰时段?当我运行代码时它只显示“NaN”,有人可以指导我吗我的代码有什么问题? The code that i am using can be seen below:我正在使用的代码如下所示:

df.loc[(df['hour'] <7) & (df['hour']>10) & (df['hour'] <3) & (df['hour']>6) , 'Rush_hour?'] = 'No' 
df.loc[(df['hour'] >=3) & (df['hour']<=6) & (df['weekday?'] == 'yes'), 'Rush_hour?'] = ' Yes-pm'  ```


Your syntax is correct and the code works.您的语法正确并且代码有效。 The problem is as ansev stated, your condition never yields True and therefore the only values you see are NaN.问题正如ansev所说,您的条件永远不会产生 True,因此您看到的唯一值是 NaN。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM