I have a dataframe where I would like to add a full address column, which would be the combination of 4 other columns (street, city, county, postalcode) from that dataframe. Example output of the address column would be:
5 Test Street, Worthing, West Sussex, RH5 3BX
Or if the city was empty as an example:
5 Test Street, West Sussex, RH5 3BX
This is my code, which after testing I see I might need to use something like apply, but I can't workout how to do it.
def create_address(street: str, city: str, county: str, postalcode: str) -> str:
list_address = []
if street:
list_address.append(street)
if city:
list_address.append(city)
if county:
list_address.append(county)
if postalcode:
list_address.append(postalcode)
address = ", ".join(list_address).rstrip(", ")
return address
df["address"] = create_address(df["Street"], df["City"], df["County"], df["PostalCode"])