Generation

generate functionMon, 13 Feb 2023

There is df2 Pandas dataframe: surgery age rectal_temp pulse respiratory_rate extremities_temp pain outcome 4 2.0 1 NaN NaN NaN 2.0 2.0 1.0 6 1.0 1 NaN 60.0 NaN 3.0 NaN 2.0 7 2.0 1 NaN 80.0 36.0 3.0 4.0 3.0 15 1.0 9 NaN 128.0 36.0 3.0 4.0 2.0 27 1.0 1 NaN NaN NaN NaN NaN 2.0 33 1.0 1 NaN 100.0 30.0 3.0 5.0 1.0 34 2.0 1 NaN 104.0 24.0 4.0 4.0 3.0 39 1.0 1 NaN 88.0 NaN 3.0 5.0 2.0 Write a function that: 1) takes as a parameter the column name (default = 'rectal_temp') 2) calculate the correlation of this column with other columns: pulse, respiratory_rate, extremities_temp, pain_outcome 3) fill all NaNs in the 'rectal_temp' column with the average values multiplied by the correlation weight

def fill_rectal_temp(df, col_name = 'rectal_temp'): corr = df.corr()[col_name][1:] corr_dict = corr.to_dict() corr_dict.pop('surgery') corr_dict.pop('outcome') corr_dict.pop('age') corr_dict.pop('pain') mean_col1 = df[col_name].mean() df_copy = df.copy() df_copy[col_name] = df_copy[col_name].fillna(0) for key, value in corr_dict.items(): col_mean = df[key].mean() df_copy[key] = df_copy[key].fillna(mean_col1*value) df_copy[col_name] = df_copy[col_name] + value*df_copy[key] return df_copy fill_rectal_temp(df2)

Questions about programming?Chat with your personal AI assistant