I am trying to generate summary statistics for a very large dataframe of hundreds of columns and summarize their relationship with an "outcome" of interest. A simplified dataframe for this can be generated using the code below:
df1 = pd.DataFrame({"time1": [0, 1, 1, 0, 0],
"time2": [1, 0, 0, 0, 1],
"time3": [0, 0, 0, 1, 0],
"outcome": [1, 0, 0, 1, 0]})
What I want to do is determine for each column their relationship with the outcome feature in terms of proportion and sum.
Currently, I am doing this a few columns at a time as follows:
df1 = df1.groupby("outcome")[["time1", "time2", "time3"]].agg(["mean", "sum"]).reset_index()
df1[df1["outcome"] == 1].T
This results in a fairly confusing dataframe as follows:
1
outcome 1.0
time1 mean 0.0
sum 0.0
time2 mean 0.5
sum 1.0
time3 mean 0.5
sum 1.0
How can I improve this output to show for each column the mean and sum in individual columns? Something like the output shown below.
mean sum
time1 0 0
time2 0.5 1
time3 0.5 1
Ideally I would like to do this for hundreds of columns in my dataframe and evaluate their relationship with outcome.
So can somebody please guide me to a solution that would allow me to do this for hundreds of columns (without typing their names individually would be the solution) and have the result in a clean dataframe as shown in example output above? Much appreciated!