dfに重複データがなければ、pivotが利用可能
def _presence_from_events(
df: pd.DataFrame,
*,
event: str,
time_col: str = "datetime",
ip_col: str = "ip",
event_col: str = "event",
) -> pd.DataFrame:
"""
Event DataFrame → Presence(bool)
指定したイベント名の Presence Mask を作成する。
Parameters
----------
df : pd.DataFrame
元データ
name : str
イベント名
例:
"ASSURED"
"SYN_RECV"
"INVALID_CLIENTHELLO"
"isolate"
event_col : str
イベント名の列
time_col : str
時刻列
ip_col : str
IP列
Returns
-------
pd.DataFrame
index : datetime
columns : ip
values : bool
"""
return (
df.loc[df[event_col] == event]
.assign(present=True)
.pivot_table(
index=time_col,
columns=ip_col,
values="present",
aggfunc="any",
fill_value=False,
)
.astype(bool)
.sort_index()
)
'''
return (
df.loc[df[event_col] == event, [time_col, ip_col]]
.assign(present=True)
.pivot(
index=time_col,
columns=ip_col,
values="present",
)
.notna()
.sort_index()
)
'''
dfに重複データがなければ、pivotが利用可能
def _presence_from_events(
df: pd.DataFrame,
*,
event: str,
time_col: str = "datetime",
ip_col: str = "ip",
event_col: str = "event",
) -> pd.DataFrame:
"""
Event DataFrame → Presence(bool)