Answers for "def conditional_impute(input_df, choice='median')"

0

def conditional_impute(input_df, choice='median')

def conditional_impute(input_df, choice='median'):
    new_df = input_df.copy()
    
    if choice == 'median':
        new_df['Age'] = round(new_df.groupby(['Sex', 'Pclass'])['Age'].transform(func = lambda x: x.fillna(x.median())),1)
        
    elif choice == 'mean':
        new_df['Age'] = round(new_df.groupby(['Sex', 'Pclass'])['Age'].transform(func = lambda x: x.fillna(x.mean())),1)
        
    else:
        raise ValueError('Please choose either median or mean as your impute choice.')
    
    return new_df
Posted by: Guest on February-07-2021
0

def conditional_impute(input_df, choice='median')

if choice == 'median':
        new_df['Age'] = new_df.groupby(['Sex', 'Pclass'])['Age'].transform(func = lambda x: x.fillna(round(x.median(),1)))
Posted by: Guest on February-07-2021

Python Answers by Framework

Browse Popular Code Answers by Language