def impute(col): if col.apply(numpy.isreal).all(axis = 0): value = numpy.nanmedian(col) else: value = col.mode().iloc[0] return col.fillna(value)