Last active
March 10, 2021 01:37
-
-
Save lisez/356b4112cfc5f4ce8fb205dfc031dcd9 to your computer and use it in GitHub Desktop.
Shopee 2021 #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import pandas as pd | |
import numpy as np | |
filepath = "/content/contacts.json" | |
with open(filepath, "r") as raw: | |
df = pd.DataFrame(json.load(raw)).set_index("Id") | |
fields = ["Email", "Phone", "OrderId"] | |
gp_result = pd.Series(df.index, index=df.index, dtype=str) | |
for gp in fields: | |
groups = df[df[gp] != ""].groupby(gp) | |
for idx, (k, v) in enumerate(groups): | |
gp_idx = idx | |
prev_values = gp_result.loc[v.index].dropna().values | |
if len(prev_values): | |
gp_idx = prev_values.min() | |
gp_result.loc[v.index] = gp_idx | |
df["gid"] = gp_result | |
output_col = pd.Series(index=df.index, dtype=str) | |
for _, rows in df.groupby("gid").Contacts: | |
keys = "-".join((str(x) for x in rows.index.tolist())) | |
output_col[rows.index] = f"{keys}, {rows.sum()}" | |
df["Output"] = output_col | |
df[["Output"]].to_csv("result.txt") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import pandas as pd | |
import numpy as np | |
filepath = "/content/contacts.json" | |
with open(filepath, "r") as raw: | |
df = pd.DataFrame(json.load(raw)).set_index("Id") | |
gp_result = pd.Series(df.index, index=df.index, dtype=str) | |
for gp in ["Email", "Phone", "OrderId"]: | |
for _, v in (df[df[gp] != ""].groupby(gp)): | |
gp_idx = v.index | |
indices = ",".join(gp_result.loc[v.index].values).split(",") | |
prev_values = pd.Index(indices, dtype=int) | |
if len(prev_values.intersection(gp_idx)): | |
gp_idx = prev_values.union(gp_idx) | |
gp_result.loc[gp_idx] = ','.join(gp_idx.astype(str).tolist()) | |
df["gid"] = gp_result | |
output_col = pd.Series(index=df.index, dtype=str) | |
for _, rows in df.groupby("gid").Contacts: | |
keys = "-".join((str(x) for x in rows.index.tolist())) | |
output_col[rows.index] = f"{keys}, {rows.sum()}" | |
df["ticket_trace/contact"] = output_col | |
df[["ticket_trace/contact"]].to_csv("result.txt") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment