Generation

generate functionThu, 16 Feb 2023

There is a Pandas Series column: 'loan_amnt' 10 78 54 GOOD 64 23 Write a function that replaces all found strings with letters in the column with np.nan. Example: replace_non_numbers(df, 'loan_amnt') df Result: 10 78 54 NaN 64 23

def replace_non_numbers(df, col): df[col] = df[col].replace(df[col][df[col].str.contains('[^0-9]')], np.nan)

Questions about programming?Chat with your personal AI assistant