0

I could not find an answer to the following problem: I have a Dataframe with several columns, e.g., name, class of a student, subjects and grades. The data is in the following form:

Name | Class | Subjects | Grades

John | 5 | [Maths, English, Chemistry] | [A, C, B]

Elliot | 4 | [English, Sports, Physics] | [B, B, A]

Hence, I have arrays in some rows. Now, I want to get a dataframe that looks as follows:

Name | Class | Subjects | Grades

John | 5 | Maths | A

John | 5 | English | C

John | 5 | Chemistry| B

Elliot | 4 | English | B

Elliot | 4 | Sports | B

Elliot | 4 | Physics | A

Hence, the arrays should be converted into several rows. The arrays have the same length. Other values, such as the Name or the Class should be simply repeated in each row.

How can I do this in python?

data = {'Name':['John', 'Elliot'], 'Class': ['5', '4'], 'Subjects': [['Maths', 'English', 'Chemistry'], ['English', 'Sports', 'Physics']], 'Grades': [['A', 'C', 'B'],['B','B','A']]}

df = pd.DataFrame(data)

print(df)
Judith
  • 57
  • 3
  • 8

0 Answers0