Generation

generate functionThu, 16 Feb 2023

Write a function that loops through the columns in the dataframe and applies try .astype('float64') to them. If a string value is encountered, it will be replaced by NaN.

def convert_float(df, cols): for col in cols: try: df[col] = df[col].astype('float64') except ValueError: df[col] = np.nan return df

Questions about programming?Chat with your personal AI assistant