Have a Dataframe:
Column_A | Column_B |
---|---|
1 | 20 |
2 | 25 |
1 | 52 |
2 | 22 |
4 | 67 |
1 | 34 |
3 | 112 |
5 | 55 |
4 | 33 |
5 | 87 |
1 | 108 |
Looking to create 2 groups from Column_A, and find the average of those groups in Column_B:
So first group might be 1, 2 and 3, second group 4 and 5.
I get the basics behind groupby()
df.groupby("Column_A")["Column_B"].mean()
and calling certain values in columns
df[df["Column_A"] == 1].groupby()[].mean()
But is there a way to include the group of (1, 2 and 3) and (4, 5) from Column_A? Somehow doing:
[["Column_A"] == 1, 2, 3].groupby(Column_B).mean()
And:
[["Column_A"] == 4, 5].groupby(Column_B).mean()
Thanks in advance