Start with lambda . Full expression:
lambda x: (0, 1)[x['workingday'] == 1 and x['humidity'] >= 60]
and this is an anonymous function that takes one argument x and returns:
1 if x['workingday'] == 1 and x['humidity'] >= 600 otherwise
the trick (0, 1)[...] used to return 0 or 1 instead of the Python False and True buffers. It uses the fact that False and True will be forced to numeric 0 and 1 when used instead of a numeric value, for example. as an index of an array (or tuple). For example, if an expression evaluates to True , cell 1 tuple that contains 1 refers to it.
This function is displayed on each row (Pandas?) Of the DataFrame (in fact, only in the filtered columns 'humidity' and 'workingday' ), and the result is stored in the column 'sticky' . However, you can translate the same expression into R using anonymous function and apply :
df$sticky <- apply(df[, c("workingday", "humidity")], 1, function(x) { x["workingday"] == 1 & x["humidity"] >= 60; });
(filtering is probably not needed, but my R skills are pretty rusty).
However, there is a more idiomatic way to achieve the same as kdopen wrote:
df$sticky <- df$workingday == 1 & df$humidity >= 60
Stefano sanfilippo
source share