mardi 23 mars 2021

How do you check for the presence of a string fomat using an if statement and len function?

I'm trying to use a len function within an if statement that write a function that will add the country of birth, "USA", to any place of birth ending with two characters (e.g. Los Angeles, Ca). The data has been read in from a csv file and I'm trying to change the column named 'birthplace'.

def cleanBP(bplace):
    string = bplace.split(", ")
    if(len(string[1]) == 2):
        string[1] = string[1] + ", USA"
        bplace = string[0] + ", " + string[1] 
    return bplace

Unfortunately I keep coming up with an error on this line: 57 data['birthplace'] = data['birthplace'].apply(cleanBP) The error says: IndexError: list index out of range and I'm not sure how to correct this. Would anyone be able to help?

Aucun commentaire:

Enregistrer un commentaire