Is there a way I can accurately develop an identifier or id for each parent in the example dataset using python?

I’m currently trying to clean up a database and create unique ids for each parent. For each parent there’s been duplicate accounts that have been created – one for each of their students. For example, a parent enters in their name one year with an alias and the next without, those are seen as two different accounts. These ids would allow me to create one parent account for all of their kids.

Example table of dummy data given below. This data includes student ids, parent names, relationships, phone numbers, emails, and addresses and a couple of other things. My issue is trying to combine parents such as alice johnson into one identifier using python and pandas.

Dummy Parent Data

I’m having trouble accurately determining one identifier for each parent without overwriting parent data or missing parents that have aliases.

So far I’ve read the data into a dataframe. I’ve preprocessed the data to try to minimize duplicates by setting strings to lowercase and stripping spaces. Below I’ve also melted the data to combine phonenumber columns into one column for each student. I’ve removed duplicates from all columns.

data = pd.read_csv(
    "archive/pall_cust.csv"
)

data["street"] = data["street"].str.lower()
data["email"] = data["email"].str.lower()
data["street"] = data["street"].astype("string")
data["parent_name"] = data["parent_name"].astype("string")
data["parent_name"] = data["parent_name"].str.strip()
data["parent_name"] = data["parent_name"].str.title()

phone_df = pd.melt(
    data,
    id_vars=["dcid", "priority"],
    value_vars=["priphone", "workphone", "cell"],
    var_name="phonetype",
    value_name="phonenumberasentered",
)
phone_df.sort_values("dcid")

phone_df.dropna(subset=["phonenumberasentered"], inplace=True)

dup = phone_df.duplicated(
    subset=["dcid", "priority", "phonenumberasentered"], keep="first"
)
phone_df.loc[dup, ["phonetype", "phonenumberasentered"]] = None
phone_df.to_csv("phone.csv")

merged_df = pd.merge(data, phone_df, on=["priority", "dcid"], how="left").drop(
    columns=["priphone", "workphone", "cell", "entry_date", "school"]
)

merged_df.drop_duplicates(
    subset=[
        "parent_name",
        "dcid",
        "liveswith",
        "legalguardian",
        "rel",
        "email",
        "phonenumberasentered",
        "street",
        "city_state_zip",
        "ln",
        "fn",
        "studentnumber",
    ],
    inplace=True,
)

This is the trouble area. I can’t figure out how to set a unique parent id. Below is what I’ve tried but grouping by priority, rel, street, city_state_zip, and phonenumberasentered and then taking the max student id from that grouping to identify parents I’ve found overwrites some parents names in my data set. I can’t use email because some parents with different names, i.e. mother and father use the same email address.

max_student = merged_df.loc[
    merged_df.groupby(
        ["priority", "rel", "street", "city_state_zip", "phonenumberasentered"]
    )["dcid"].idxmax()
]

parent_names_from_max = max_student.set_index(
    ["priority", "rel", "street", "city_state_zip", "phonenumberasentered"]
)["parent_name"]

merged_df["parent_name"] = merged_df.set_index(
    ["priority", "rel", "street", "city_state_zip", "phonenumberasentered"]
).index.map(parent_names_from_max)
merged_df.reset_index(drop=True, inplace=True)

parent_to_id = {
    value: idx + 1 for idx, value in enumerate(merged_df["parent_name"].unique())
}
merged_df.loc[:, "New Contact Identifier"] = merged_df["parent_name"].map(parent_to_id)

Finally, I’ve tried first using things like Spacy or Levenshtein distance to try to select similar parent names but I’ve found that doesn’t work efficiently enough. Thanks for your ideas and help.

New contributor

Chase Brown is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật