Skip to content

Instantly share code, notes, and snippets.

@jmccardle
Created May 13, 2023 21:16
Show Gist options
  • Save jmccardle/9457ac828f7a5d77872fe09c901322a7 to your computer and use it in GitHub Desktop.
Save jmccardle/9457ac828f7a5d77872fe09c901322a7 to your computer and use it in GitHub Desktop.
import pandas as pd
def region(row):
#print(row)
state = row["state"]
if state in ("FL", "GA"): return "South"
elif state in ("CA", "WA"): return "West"
elif state in ("MT", "MN"): return "Central"
elif state in ("NY", "ME"): return "North"
else: return "Unknown"
#example data since I don't have your db.
df = pd.DataFrame(data={"d": [1,4,9,16], "state":["NY", "FL", "CA", "MT"]})
regions = df.apply(region, axis=1)
df["region"] = regions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment