Home > Net >  How can you apply a specific custom function to a specific column in pandas dataframe and store the
How can you apply a specific custom function to a specific column in pandas dataframe and store the

Time:07-11

I am trying to call a specific function passing it each row of a particular column in a dataframe (df) and then store the return of that function in another dataframe (df3).

This is the function:

    import pandas as pd
    import datetime
    import re                             #enables carrier lookup

    def recognize_delivery_service(tracking):
    service = None

usps_pattern = [
    '^(94|93|92|94|95)[0-9]{20}$',
    '^(94|93|92|94|95)[0-9]{22}$',
    '^(70|14|23|03)[0-9]{14}$',
    '^(M0|82)[0-9]{8}$',
    '^([A-Z]{2})[0-9]{9}([A-Z]{2})$'
]

ups_pattern = [
    '^(1Z)[0-9A-Z]{16}$',
    '^(T) [0-9A-Z]{10}$',
    '^[0-9]{9}$',
    '^[0-9]{26}$'
]

fedex_pattern = [
    '^[0-9]{20}$',
    '^[0-9]{15}$',
    '^[0-9]{12}$',
    '^[0-9]{22}$'
]

usps = "("   ")|(".join(usps_pattern)   ")"
fedex = "("   ")|(".join(fedex_pattern)   ")"
ups= "("   ")|(".join(ups_pattern)   ")"

if re.match(usps, tracking) != None:
    service = 'USPS'
elif re.match(ups, tracking) != None:
    service = 'UPS'
elif re.match(fedex, tracking) != None:
    service = 'FedEx'

return service

    #Pass tracking to function

    temp_track = None
    temp_service = None

    for i in range(len(df)):
        temp_track = df.iloc[i, 44]                           #tracking num located in column 44
    temp_service = recognize_delivery_service(temp_track) #pass track num to function
    df3.iloc[i, 0] = temp_service                         #store service value in column 0 of df3

This is one of many attempts I've made but can't get the function to return proper service nor store in df3.

CodePudding user response:

I don't really understood what this function should do, but I think pandas.apply might be relevant... https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.apply.html

if you could give an example of input and desired output I could help you more.

  • Related