python - Nested ifelse alternative in pandas -


suppose we've got test dataset:

value   group 123 1 120 1 na  1 130 1 23  2 22  2 24  2 na  2 

now want replace missing values group-wise median values. in r can using 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 applying numpy.where function or pandas.dataframe.set.map method showcased here, both techniques not support nesting. can think of list comprehension this, wish know if there alternative in realm of numpy/pandas. thank in advance.

in case, can use groupby fill group median:

in [16]: df.groupby('group')['value'].apply(lambda x: x.fillna(x.median())) out[16]:  0    123 1    120 2    123 3    130 4     23 5     22 6     24 7     23 dtype: float64 

although in general, both of methods can nested fine. e.g., do:

in [23]: medians = df.groupby('group')['value'].median()  in [24]: np.where(pd.isnull(df['value']),             np.where(df['group'] == 1, medians.loc[1], medians.loc[2]),                df['value']) out[24]: array([ 123.,  120.,  123.,  130.,   23.,   22.,   24.,   23.]) 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -