简体   繁体   中英

Python incorrect result from function returned in dataframe

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

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))

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']))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM