#NEWLINE# def rare_category_replacement( data , column , replace_col , cutoff = 3 ) : #NEWLINE# #INDENT# '\n    Replace any categorical values that occur less than cutoff times \n    in data[column] with data[replace_col]\n    \n    Args:\n        data - a pandas DataFrame\n        column - the name of the column that is being thresholded\n        replace_col - the name of the column that is used for replacement values\n        cutoff - the cutoff threshold for replacement\n    \n    Returns:\n        a DataFrame that has any values in data[column] that occur less than\n        cutoff time replaced with data[replace_col]\n    ' #NEWLINE# counts = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# data [ column ] = data [ column ] . astype ( 'S100' ) #NEWLINE# data [ replace_col ] = data [ replace_col ] . astype ( 'S100' ) #NEWLINE# for idx in data . index : #NEWLINE# #INDENT# counts [ data [ column ] [ idx ] ] = ( counts . get ( data [ column ] [ idx ] , 0 ) + 1 ) #NEWLINE# #UNINDENT# for idx in data . index : #NEWLINE# #INDENT# if ( counts [ data [ column ] [ idx ] ] < cutoff ) : #NEWLINE# #INDENT# data [ column ] [ idx ] = data [ replace_col ] [ idx ] #NEWLINE# #UNINDENT# #UNINDENT# return data