DataFrame Pandas - Calculate column based on other

2

I have a dataframe in the following format:

colunas = [
    'COMEDY',
    'CRIME',
    'Classe Prevista'
]

precisao_df = pd.DataFrame(columns=colunas)
precisao_df['COMEDY'] = y_pred_proba[:,0]
precisao_df['CRIME'] = y_pred_proba[:,1]
precisao_df

And I want to do the following: for each line, if comedy > crime, expected class = comedy, otherwise crime. That is, the predicted class is what my model was most likely to be. How can I make this expression?

Ex:

    COMEDY    CRIME      Classe prevista
0    0.9       0.1          COMEDY
1    0.42      0.58         CRIME
    
asked by anonymous 31.10.2018 / 05:31

2 answers

2

Create a function that determines the class you want to place in the column:

def acha_classe(registro):
    if registro['COMEDY'] > registro['CRIME']:
        return 'COMEDY'
    elif registro['COMEDY'] < registro['CRIME']:
        return 'CRIME'
    else:
        return 'UNKNOWN'

Just now apply ( apply ) this function:

df['Classe Prevista'] = df.apply(acha_classe, axis=1)
    
31.10.2018 / 05:56
2

You can use the where

31.10.2018 / 06:06