justanewb
justanewb

Reputation: 133

How to use the apply function for a custom function in python

I have this function

def cleanup_model(model, clean_model_list):
    model = model.lower()
    clean_model_list = [x.lower() for x in clean_model_list]
    for x in clean_model_list:
        if x in model:
            return x
    return model

I have this dataframe

d = {'laptops' : ['apple macbook pro 16','apple macbook', 'hp laptop xyz 15']}
a = pd.DataFrame(data=d)

I have this list

l = ['apple macbook', 'hp laptop']

I want to use the apply function in pandas to get this

d = {'laptops' : ['apple macbook','apple macbook', 'hp laptop xyz 15']}
a = pd.DataFrame(data=d)

I am a bit confused how to apply the function above to the entire dataframe. Any suggestions are appreciated.

Upvotes: 1

Views: 87

Answers (2)

tomjn
tomjn

Reputation: 5389

You can pass arguments to apply (in this case using Series.apply) using the args argument, or by passing your keyword arguments. In your case

def cleanup_model(model, clean_model_list):
    model = model.lower()
    clean_model_list = [x.lower() for x in clean_model_list]
    for x in clean_model_list:
        if x in model:
            return x
    return model

d = {'laptops' : ['apple macbook','apple macbook', 'hp laptop xyz 15']}
a = pd.DataFrame(data=d)
l = ['apple macbook', 'hp laptop']
a.laptops.apply(cleanup_model, args=(l,))

which will give you

0    apple macbook
1    apple macbook
2        hp laptop
Name: laptops, dtype: object

You can also just use keyword arguments like so

a.laptops.apply(cleanup_model, clean_model_list=l)

Upvotes: 2

Heinrich
Heinrich

Reputation: 398

dataframe.apply(f) is used when you have a function that takes one parameter and gives back one output

currently, your function takes in 2 parameters, so .apply(cleanup_model) won't work.

If you know what clean_model_list's value is before hand, I suggest doing this: (assuming that l = ['apple macbook', 'hp laptop'] is your clean_model_list

def cleanup_model(model, clean_model_list=['apple macbook', 'hp laptop']):
    model = model.lower()
    clean_model_list = [x.lower() for x in clean_model_list]
    for x in clean_model_list:
        if x in model:
            return x
    return model

a.apply(cleanup_model)

alternatively, you could go

clean_model_list=['apple macbook', 'hp laptop']

def cleanup_model(model):
    model = model.lower()
    clean_model_list = [x.lower() for x in clean_model_list]
    for x in clean_model_list:
        if x in model:
            return x
    return model

a.apply(cleanup_model)

Upvotes: 1

Related Questions