I have the following info in a dataframe in python:
From that dataframe, I require another dataframe that summarizes the information of all the students per state per age.
In other words, instead of having one entry per student (with their age and state) I'd like to have a data frame with the rows as the states and the columns as the ages.
Student | State | Age |
---|---|---|
1 | California | 13 |
2 | California | 14 |
3 | Colorado | 12 |
4 | Colorado | 11 |
5 | Colorado | 12 |
6 | Colorado | 10 |
7 | Colorado | 13 |
8 | Colorado | 12 |
9 | Colorado | 13 |
10 | Colorado | 11 |
11 | Florida | 10 |
12 | Florida | 11 |
13 | Texas | 11 |
14 | Texas | 9 |
15 | Texas | 12 |
16 | Texas | 10 |
This is what I am expecting
State | 9 | 10 | 11 | 12 | 13 | 14 |
---|---|---|---|---|---|---|
California | 0 | 0 | 0 | 0 | 1 | 1 |
Colorado | 0 | 1 | 2 | 3 | 2 | 0 |
Florida | 0 | 1 | 1 | 0 | 0 | 0 |
Texas | 1 | 1 | 1 | 1 | 0 | 0 |
And I need it through iterations because In reality I have thousands of rows and dozens of different variables
Do you know what I can do to achieve this?