How to perform a rowwise function on a column of data and append the output of the function to a Pandas data.frame?

Viewed 46

I am very new to python (I generally use R). I have a list of addresses that I need to normalize. I would like to do the following.

  1. Normalize each address in the list using the scourgify package
  2. Append the data to my original data.frame.

On github, it is easy to see how to do one address, but how would I do this to a list or vector of addresses?

from scourgify import normalize_address_record

normalize_address_record('123 southwest Main street, Boring, or, 97203')

normalize_address_record({
    'address_line_1': '123 southwest Main street',
    'address_line_2': 'unit 2,
    'city': 'Boring',
    'state': 'or',
    'postal_code': '97203'
})
expected output

{
     'address_line_1': '123 SW MAIN ST',
     'address_line_2': 'UNIT 2'
     'city': 'BORING',
     'state': 'OR',
     'postal_code': '97203'
 }

Here is some dummy data based on my original dataset

# initialize list of lists
data = [['a', '123 southwest Main street, Boring, or, 97203'], ['b', '4285 balsam la n plymouth mn 55441'], ['c', '632 bloomington ave s minneapolis mn 55417']]
 
# Create the pandas DataFrame
df = pd.DataFrame(data, columns = ['id', 'address_original'])

I need the output in tabular format appended to my original data.frame. Any help would be greatly appreciated.

1 Answers

Try:

from scourgify import normalize_address_record

data = [
    ["a", "123 southwest Main street, Boring, or, 97203"],
    ["b", "4285 balsam la n plymouth mn 55441"],
    ["c", "632 bloomington ave s minneapolis mn 55417"],
]

# Create the pandas DataFrame
df = pd.DataFrame(data, columns=["id", "address_original"])

df_out = pd.concat(
    [
        df,
        df["address_original"].apply(normalize_address_record).apply(pd.Series),
    ],
    axis=1,
)
print(df_out)

Prints:

  id                              address_original         address_line_1 address_line_2         city state postal_code
0  a  123 southwest Main street, Boring, or, 97203         123 SW MAIN ST           None       BORING    OR       97203
1  b            4285 balsam la n plymouth mn 55441       4285 BALSAM LA N           None     PLYMOUTH    MN       55441
2  c    632 bloomington ave s minneapolis mn 55417  632 BLOOMINGTON AVE S           None  MINNEAPOLIS    MN       55417

EDIT: To handle errors gracefully:

def fn(x):
    try:
        return normalize_address_record(x)
    except:
        return {}


df_out = pd.concat(
    [
        df,
        df["address_original"].apply(fn).apply(pd.Series),
    ],
    axis=1,
)
print(df_out)
Related