I had a dataframe that looked like this:
| id | stu1 | stu2 |
|---|---|---|
| 11 | James | Rob |
| 22 | Bob | Tom |
| 33 | Ryan | Ian |
| 44 | Oliver | Saj |
| 55 | Burt | Ben |
| 66 | Chris | Rich |
To combine stu1 and stu2 into a single column with each name on a new row (and duplicating id for each name) I used this:
stu_df = stu_df.set_index('id').stack().reset_index(level=1, drop=True).to_frame('stu').reset_index()
Which gave this result:
| id | stu |
|---|---|
| 11 | James |
| 11 | Rob |
| 22 | Bob |
| 22 | Tom |
| 33 | Ryan |
| 33 | Ian |
| 44 | Oliver |
| 44 | Saj |
| 55 | Burt |
| 55 | Ben |
| 66 | Chris |
| 66 | Rich |
The original dataframe has been updated to include dates and looks like this:
| date | id | stu1 | stu2 |
|---|---|---|---|
| 03/06/2022 | 11 | James | Rob |
| 03/06/2022 | 22 | Bob | Tom |
| 03/06/2022 | 33 | Ryan | Ian |
| 03/06/2022 | 44 | Oliver | Saj |
| 03/06/2022 | 55 | Burt | Ben |
| 03/06/2022 | 66 | Chris | Rich |
I want to combine stu1 and stu2 in the same way (duplicating the date and id for each name) but can't get it to work.
The desired outcome is this:
| date | id | stu |
|---|---|---|
| 03/06/2022 | 11 | James |
| 03/06/2022 | 11 | Rob |
| 03/06/2022 | 22 | Bob |
| 03/06/2022 | 22 | Tom |
| 03/06/2022 | 33 | Ryan |
| 03/06/2022 | 33 | Ian |
| 03/06/2022 | 44 | Oliver |
| 03/06/2022 | 44 | Saj |
| 03/06/2022 | 55 | Burt |
| 03/06/2022 | 55 | Ben |
| 03/06/2022 | 66 | Chris |
| 03/06/2022 | 66 | Rich |
I'm new to Pandas and am struggling. Can anyone help? Thanks