How can I use the ternary operator in a lambda function in applya pandasdataframe function ?
First of all, this code is from R / plyr, which is what I want to get:
ddply(mtcars, .(cyl), summarise, sum(ifelse(carb==4,1,0))/sum(ifelse(carb %in% c(4,1),1,0)))
in the function above, I can use the function ifelse, the three-dimensional operator R, to compute the resulting data frame.
However, when I want to do the same in Python / pandas with the following code
mtcars.groupby(["cyl"]).apply(lambda x: sum(1 if x["carb"] == 4 else 0) / sum(1 if x["carb"] in (4, 1) else 0))
The following error occurs:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
So, how can I calculate and get the same data file as in R / plyr?
For your information, if I use a ternary operator without grouping columns, for example
mtcars.apply(lambda x: sum(1 if x["carb"] == 4 else 0) / sum(1 if x["carb"] in (4, 1) else 0), axis=1)
I can get the resulting data frame for some reason (but that’s not what I wanted to do).
Thanks.
[Update]
, , 1 0, . , R/plyr :
ddply(mtcars, .(cyl), summarise, sum(ifelse(carb==4,6,3))/sum(ifelse(carb %in% c(4,1),8,4)))
?