def factorize(train, test, features, na_value=-9999, full=False, sort=True):
    for column in features:
        if full:
            vs = pd.concat([train[column], test[column]])
            labels, indexer = pd.factorize(vs, sort=sort)
        else:
            labels, indexer = pd.factorize(train[column], sort=sort)
        train[column] = indexer.get_indexer(train[column])
        test[column] = indexer.get_indexer(test[column])
        if na_value != -1:
            train[column] = train[column].replace(-1, na_value)
            test[column] = test[column].replace(-1, na_value)
    return train, test