0

I am trying to create a simple data frame that imports a CSV file containing 3 columns ['Date','Amount','Description'] being banking transactions. and then applies a code. I have created a simple function to return the code.

   def codelookup(string):
        code_dict = {'GOLFBOX':'Golf Clubs','HARVEY NORMAN': 'TECH','AMAZON': 'TECH'}
        for code in code_dict:
            if code in string:
                return str(code_dict[code])
                break
        else:
                return 'Other'

    df_data = pd.DataFrame({'Date': ['28/12/18','28/12/18','27/12/18'], 
                       'Amount': [-307.99,-14,-43.86], 
                       'Description': ['GOLFBOX OSBORNE PARK OSBORNE PARK','CLUBLINKS MANAGEMENT P COMO','WOOLWORTHS 4301 PERTH']})
    df_data["Code"] = codelookup(df_data['Description'])
    df_data

The result I am returned with the function is 'Other', in each of the 3 transactions. It's not correctly sending the 'Description' to the function as the function works with a simple call.

I'm a newbie so apologies for the description of my issue, be keen to see a cleaner way to do this lookup.

Regards JDLove

2 Answers 2

1

Use the

apply

method. You need to call this function for each row one by one, instead of passing Pandas Series at a go. Try this:

df_data["Code"] = df_data['Description'].apply(lambda x: codelookup(x))
Sign up to request clarification or add additional context in comments.

1 Comment

Fantastic, thanks for the advice it works perfectly and really is quite cool.
0

You are sending ['GOLFBOX OSBORNE PARK OSBORNE PARK','CLUBLINKS MANAGEMENT P COMO','WOOLWORTHS 4301 PERTH'] as parameter to the codelookup in which is list not string,you are using condition if code in string: which actually looks for the word in the above list if matches any record(not each words in single record). You can try converting the list in string and then try:

codelookup(str(df_data['Description']))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.