我有以下代码:
data['ShortLongFlag'] = data['End DateTime'].apply(lambda x:
-1 if (x.month == 3 and x.date() in shortlongdates == True) else (1 if (x.month == 10 and x.date() in shortlongdates == True) else 0))
我想做的是:
在my dataframe中创建一个新列,该列根据以下条件填充-1、0或1:
现在所有的值都在新列中以0的形式输出。。。为什么?
所以我们有一个不可读的东西:
让我们把它写成一个标准函数:
删除一些奇怪的测试(冗余==True)和括号
{}真的是可调用的吗?应该是
x.date
吗? 如果没有看到你的data
和listofdates
,就不可能说这可能会失败,但至少我们可以看到它在做什么这个问题的原因是chaining comparison operators
比较,包括成员资格测试和身份测试have same precedence
即
与
注意,它可以写为
x.month == 3 and x.date() in shortlongdates
,也可以使用括号。但是,正如注释中所述,最好将此lambda编写为常规函数。如果你坚持的话,我会让你把这个转换回[更简单的]lambda
相关问题 更多 >
编程相关推荐