Skip to content

Commit

Permalink
parent_code_table_to_parent_id_table now more performant on larger
Browse files Browse the repository at this point in the history
datasets.
  • Loading branch information
makmanalp committed Jul 8, 2015
1 parent f96dd3f commit 1b59195
Showing 1 changed file with 4 additions and 13 deletions.
17 changes: 4 additions & 13 deletions classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,11 @@ def parent_code_table_to_parent_id_table(df, hierarchy):
"""From a classification that has parent_code, go to one that has
parent_id."""

def replace(x):
parent_level = hierarchy.parent(x.level)

if parent_level is None:
x["parent_id"] = None
else:
parent_rows = df[(df.code == x.parent_code)
& (df.level == parent_level)]
x["parent_id"] = parent_rows.index[0]

return x.drop("parent_code")

return df.apply(replace, axis=1)
code_table = df[["code"]].reset_index()
code_table.columns = ["parent_id", "parent_code"]

return df.merge(code_table, on="parent_code", how="left")\
.drop("parent_code", axis=1)

def ordered_table_to_parent_code_table(df, hierarchy):

Expand Down

0 comments on commit 1b59195

Please sign in to comment.