-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
28 lines (24 loc) · 803 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import pandas as pd
def add_contact_id(df: pd.DataFrame) -> pd.DataFrame:
# Create contact ids
df["contact_id"] = (
df["game_play"]
+ "_"
+ df["step"].astype("str")
+ "_"
+ df["nfl_player_id_1"].astype("str")
+ "_"
+ df["nfl_player_id_2"].astype("str")
)
return df
def expand_contact_id(df: pd.DataFrame, drop=True) -> pd.DataFrame:
"""
Splits out contact_id into seperate columns.
"""
df["game_play"] = df["contact_id"].str[:12]
df["step"] = df["contact_id"].str.split("_").str[-3].astype("int")
df["nfl_player_id_1"] = df["contact_id"].str.split("_").str[-2]
df["nfl_player_id_2"] = df["contact_id"].str.split("_").str[-1]
if drop:
return df.drop(columns=["contact_id"])
return df