vendredi 2 avril 2021

I want to create a new column which categorizes values from another column but i get a ValueError after applying lambda

I want to create a new column which categorizes the values in the manufacturer column to their contintent of origen e.g. Europe for Volkswagen. The values in the manufacturer columns are objects.

Im trying to use the following function:

def  func(region):
    if auto2['manufacturer'] == 'Audi':
        return 'Europa'
    elif auto2['manufacturer'] == 'Land Rover':
        return 'Europa'
    elif auto2['manufacturer'] == 'Volkswagen':
        return 'Europa'
    elif auto2['manufacturer'] == 'Chevrolet':
        return 'Verenigde Staten'
    elif auto2['manufacturer'] == 'Jeep':
        return 'Verenigde Staten'
    elif auto2['manufacturer'] == 'Dodge':
        return 'Verenigde Staten'
    elif auto2['manufacturer'] == 'Ford':
        return 'Verenigde Staten'
    elif auto2['manufacturer'] == 'Lincoln':
        return 'Verenigde Staten'
    elif auto2['manufacturer'] == 'Mercury':
        return 'Verenigde Staten'
    elif auto2['manufacturer'] == 'Pontiac':
        return 'Verenigde Staten'
    else: 
        return 'Asia'

auto2['Region'] = auto2['manufacturer'].apply(lambda x: region)

Defining the function works fine but when I execute the apply function i get the following error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Is there any way I can fix my code, or is there a better way to make a new column which categorizes values from another column?

Aucun commentaire:

Enregistrer un commentaire