1
$\begingroup$

df.features is like a list of features in each row. First I set all those encoded to 0, and then did this:

for index, row in df.features.iteritems():
if type(row) == str:
    if 'Academia' in row:
        df.academia.iloc[index] = 1
    if 'Churrasqueira' in row:
        df.churrasqueira.iloc[index] = 1
    if 'Cinema' in row:
        df.cinema.iloc[index] = 1
    if 'Espaço gourmet' in row:
        df.espaco_gourmet.iloc[index] = 1
    if 'Espaço verde / Parque' in row:
        df.parque.iloc[index] = 1
    if 'Gramado' in row:
        df.gramado.iloc[index] = 1
    if 'Jardim' in row:
        df.jardim.iloc[index] = 1
    if 'Piscina' in row:
        df.piscina.iloc[index] = 1
    if 'Playground' in row:
        df.playground.iloc[index] = 1
    if 'Quadra de tênis' in row:
        df.quadra_tenis.iloc[index] = 1
    if 'Quadra poliesportiva' in row:
        df.quadra_poli.iloc[index] = 1
    if 'Quintal' in row:
        df.quintal.iloc[index] = 1
    if 'Salão de festas' in row:
        df.salao_festas.iloc[index] = 1
    if 'Salão de jogos' in row:
        df.salao_jogos.iloc[index] = 1
    if 'Aquecimento' in row:
        df.aquecimento.iloc[index] = 1
    if 'Ar condicionado' in row:
        df.ar_condicionado.iloc[index] = 1
    if 'Conexão à internet' in row:
        df.internet.iloc[index] = 1
    if 'Depósito' in row:
        df.deposito.iloc[index] = 1
    if 'Elevador' in row:
        df.elevador.iloc[index] = 1
    if 'Garagem' in row:
        df.garagem.iloc[index] = 1
    if 'Lareira' in row:
        df.lareira.iloc[index] = 1
    if 'Lavanderia' in row:
        df.lavanderia.iloc[index] = 1
    if 'Massagem' in row:
        df.massagem.iloc[index] = 1
    if 'Mobiliado' in row:
        df.mobiliado.iloc[index] = 1
    if 'Recepção' in row:
        df.recepcao.iloc[index] = 1
    if 'Sauna' in row:
        df.sauna.iloc[index] = 1
    if 'Spa' in row:
        df.spa.iloc[index] = 1
    if 'TV a cabo' in row:
        df.tv_cabo.iloc[index] = 1
    if 'Circuito de segurança' in row:
        df.circuito_seguranca.iloc[index] = 1
    if 'Condomínio fechado' in row:
        df.condominio_fechado.iloc[index] = 1
    if 'Interfone' in row:
        df.interfone.iloc[index] = 1
    if 'Segurança 24h' in row:
        df.seguranca_24h.iloc[index] = 1
    if 'Sistema de alarme' in row:
        df.alarme.iloc[index] = 1
    if 'Vigia' in row:
        df.vigia.iloc[index] = 1
    if 'Área de serviço' in row:
        df.area_servico.iloc[index] = 1
    if 'Cozinha' in row:
        df.cozinha.iloc[index] = 1
    if 'Escritório' in row:
        df.escritorio.iloc[index] = 1
    if 'Sacada' in row:
        df.sacada.iloc[index] = 1
    if 'Varanda gourmet' in row:
        df.varanda_gourmet.iloc[index] = 1
$\endgroup$
2
$\begingroup$

The easiest and most efficient way to do it, is to use the One Hot Encoding ScikitLearn

This is an example:

>>> from sklearn.preprocessing import OneHotEncoder
>>> enc = OneHotEncoder()
>>> enc.fit([[0, 0, 3], [1, 1, 0], [0, 2, 1], [1, 0, 2]])  
OneHotEncoder(categorical_features='all', dtype=<... 'numpy.float64'>,
       handle_unknown='error', n_values='auto', sparse=True)
>>> enc.n_values_
array([2, 3, 4])
>>> enc.feature_indices_
array([0, 2, 5, 9])
>>> enc.transform([[0, 1, 1]]).toarray()
array([[ 1.,  0.,  0.,  1.,  0.,  0.,  1.,  0.,  0.]])
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.