Randy Morrison
Randy Morrison

Reputation: 95

Apply a function to data frame

I have a simple function that determines if a string contains substrings.

def scoring_names(string, substring1, substring2, substring3):
    """Simple function to calculate the substrings in a string"""
    score_list=[]
    sub1 = string.count(substring1)
    score_list.append(sub1)
    sub2 = string.count(substring2)
    score_list.append(sub2)
    sub3 = string.count(substring3)
    score_list.append(sub3)
    #print(score_list)
    return sum(score_list)

I also have a data frame:

import pandas as pd 
# data  
data = [['James', 'Bond','Crazy','james_bond_fox'],
        ['John','Smith','Blackhand','davinchi_84'], 
        ['Jose','Romero', 'Bear','jose.gamez']]

# Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['Str_1', 'Str_2', 'Str_3', 'String']) 

When I apply the function on the data frame - I see the following mistakes:

TypeError: list indices must be integers or slices, not str
AttributeError: 'RangeIndex' object has no attribute 'levels'

Can anyone suggest how I can solve the issues?

Upvotes: 0

Views: 41

Answers (1)

Andreas
Andreas

Reputation: 9197

Works for me.

df.apply(lambda x: scoring_names(x['String'],x['Str_1'],x['Str_2'],x['Str_3']),axis=1)

You might need to do some case sensitive adjustments though, .e.g. like this:

def scoring_names(string, substring1, substring2, substring3):
    """Simple function to calculate the substrings in a string"""
    string = string.lower()
    substring1 = substring1.lower()
    substring2 = substring2.lower()
    substring3 = substring3.lower()
    
    score_list=[]
    sub1 = string.count(substring1)
    score_list.append(sub1)
    sub2 = string.count(substring2)
    score_list.append(sub2)
    sub3 = string.count(substring3)
    score_list.append(sub3)
    #print(score_list)
    return sum(score_list)

Upvotes: 1

Related Questions