0

I am looking for a little bit of help. I have the dataset similar to the one below. I am looking to use pandas to capture the columns with ID #'s and make those my new rows. But I also needing to pull the Category, Time, and Date columns with that. The columns will not always have data entered in them.

Category Time Date ID1 ID2 ID3
Math 15 08/21/2021 11111 22222 33333
Reading 45 08/21/2021 11111 44444 55555
Math 30 08/25/2021 22222
Science 20 08/29/2021 33333 44444

And the output should look similar to this. Where all the columns with my ID numbers are now my rows and the Category, Time, and Date columns are repeated for that ID according to how they appeared in the original dataset.

ID Date Category Time
11111 08/21/2021 Math 15
11111 08/21/2021 Reading 45
22222 08/21/2021 Math 15
33333 08/21/2021 Math 15
44444 08/21/2021 Reading 45
22222 08/25/2021 Math 30
33333 08/29/2021 Science 20
44444 08/29/2021 Science 20

Thank you for any help with this.

markm
  • 1
  • you could have a look at the `wide_to_long` function: ``pd.wide_to_long(df, i = ['Category', 'Time', 'Date'], stubnames='ID', j = 'num', sep='').droplevel('num').dropna().reset_index()`` – sammywemmy Aug 31 '21 at 21:21

0 Answers0