Python:numpy where命令与if STATEMAN

2024-06-28 05:21:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个dataframe df,其中包含一列日期,格式为'2011-12-13',还有一列时间,同样是字符串格式,如'15:40:00'。在

测向

index                 date        time
2011-01-03 09:40:00   2011-01-03  09:40:00 
2011-01-03 09:45:00   2011-01-03  09:45:00 
2011-01-03 09:50:00   2011-01-03  09:50:00  
2011-01-03 09:55:00   2011-01-03  09:55:00 
2011-01-03 10:00:00   2011-01-03  10:00:00  
2011-01-03 10:05:00   2011-01-03  10:05:00  

我的目标是在我的数据框中创建一个列F0,其中F0=1如果日期属于这些日期中的任何一个('2011-01-26','2011-03-15', '2011-08-09', '2011-09-21', '2011-12-13'),如果{}。在

我尝试使用numpy函数where,如下所示:

^{pr2}$

我得到这个错误: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().为什么?我不知道如何正确使用any函数。在

我想为其他F2F3等创建多个列,例如:

df['F77'] = np.where((df.date == any(dates) & (df.time== '16:00:00'), 1, 0))


Tags: 数据函数字符串numpy目标dataframedfdate