Suppose we've got a test
dataset:
value group
123 1
120 1
NA 1
130 1
23 2
22 2
24 2
NA 2
Now we want to replace missing values with group
-wise median values. In R
we can do it using a nested ifelse
call.
first.med <- median(test[test$group == 1, ]$value, na.rm = T)
second.med <- median(test[test$group == 2, ]$value, na.rm = T)
test$value <- ifelse(is.na(test$value) & test$group == 1, first.med
ifelse(is.na(test$value) & test$group == 2, second.med,
test$value))
I though about applying the numpy.where
function or the pandas.DataFrame.Set.map
method as showcased here, but both techniques do not support nesting. I can think of a list comprehension to do this, but I wish to know if there is an alternative in the realm of NumPy/pandas. Thank you in advance.