Calculate common features outside if...else.

communication
junos 2021-08-18 10:54:54 +02:00
parent 4740e94d37
commit a2a44c202a
1 changed files with 8 additions and 16 deletions

View File

@ -164,16 +164,8 @@ def count_comms(comm_df: pd.DataFrame) -> pd.DataFrame:
.add_prefix("duration_max_") .add_prefix("duration_max_")
) )
# Max call duration by type # Max call duration by type
comm_contacts_counts = (
enumerate_contacts(comm_df)
.groupby(["participant_id"])
.nunique()["contact_id"]
.rename("no_contacts")
)
# Number of communication contacts
comm_features = comm_counts.join(comm_duration_total) comm_features = comm_counts.join(comm_duration_total)
comm_features = comm_features.join(comm_duration_max) comm_features = comm_features.join(comm_duration_max)
comm_features = comm_features.join(comm_contacts_counts)
try: try:
comm_features.drop(columns="duration_total_" + call_types[3], inplace=True) comm_features.drop(columns="duration_total_" + call_types[3], inplace=True)
comm_features.drop(columns="duration_max_" + call_types[3], inplace=True) comm_features.drop(columns="duration_max_" + call_types[3], inplace=True)
@ -196,16 +188,16 @@ def count_comms(comm_df: pd.DataFrame) -> pd.DataFrame:
no_sent_ratio=lambda x: x.no_sent / x.no_all, no_sent_ratio=lambda x: x.no_sent / x.no_all,
) )
# Ratio of incoming and outgoing messages to all messages. # Ratio of incoming and outgoing messages to all messages.
comm_contacts_counts = (
enumerate_contacts(comm_df)
.groupby(["participant_id"])
.nunique()["contact_id"]
.rename("no_contacts")
)
# Number of communication contacts
comm_features = comm_features.join(comm_contacts_counts)
else: else:
raise KeyError("The dataframe contains neither call_type or message_type") raise KeyError("The dataframe contains neither call_type or message_type")
comm_contacts_counts = (
enumerate_contacts(comm_df)
.groupby(["participant_id"])
.nunique()["contact_id"]
.rename("no_contacts")
)
# Number of communication contacts
comm_features = comm_features.join(comm_contacts_counts)
return comm_features return comm_features