Skip to content

Commit

Permalink
utility function to convert repeated game results to long format
Browse files Browse the repository at this point in the history
  • Loading branch information
phelps-sg committed Jan 22, 2024
1 parent 3585276 commit 8c760ae
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions llm_cooperation/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,24 @@ def save_table(df: pd.DataFrame, name: str, caption: str) -> pd.DataFrame:
renderer = df.style.format(decimal=".", thousands=",", precision=2)
renderer.to_latex(f"../latex/{name}.tex", caption=caption)
return df


def repeated_to_long(row: pd.Series) -> pd.DataFrame:
df = pd.DataFrame(row).transpose()
choices = df.Choices.values[0]
n = 6
user_choices = [None] * 6
ai_choices = [None] * 6
if choices is not None:
n = len(choices)
user_choices = [c.user for c in choices]
ai_choices = [c.ai for c in choices]
df_long = pd.concat([df] * n, axis=0)
df_long["User_choice"] = user_choices
df_long["AI_choice"] = ai_choices
df_long["Round"] = range(n)
return df_long


def repeated_to_long_format(df: pd.DataFrame) -> pd.DataFrame:
return pd.concat([repeated_to_long(df.iloc[i]) for i in range(len(df))])

0 comments on commit 8c760ae

Please sign in to comment.