dimanche 14 mars 2021

Create a True/False column in Python DataFrame(1,0) based on two columns values

I'm having trouble creating a new column based on columns 'language_1' and 'language_2' in python dataframe. I want to create a 'bilingual' column where a '1' represents a user who speaks both English and Spanish(bi-lingual) and a 0 for non-bilingual speakers. Ultimately I want to compare their average ratings to each other, but want to categorize them first. I tried using if statements but I'm not sure how to write an if statement that combines multiple conditions to result in 1 value. Thank you for any help.


===============================================================================================

name          language_1             language_2          rating      bilingual                                           
Kevin          English                 Null               4.25
Miguel         English                 Spanish             4.56
Carlos         English                  Spanish            4.61
Aaron          Null                     Spanish            4.33


===============================================================================================

Here is the code I've tried to use to append the new column to my dataframe.

def label_bilingual(row):
    if row('language_english') == row['English'] and row('language_spanish') == 'Spanish':
        val = 1
    else:
        val = 0

df_doc_1['bilingual'] = df_doc_1.apply(label_bilingual, axis=1)

Here is the error I'm getting.

----> 1 df_doc_1['bilingual'] = df_doc_1.apply(label_bilingual, axis=1)
'Series' object is not callable

Aucun commentaire:

Enregistrer un commentaire