How to start the list in a certain line through Pandas into a single line, as shown in the figure

Viewed 20

How to start the list in a certain line through Pandas into a single line, as shown in the figure. enter image description here

1 Answers

Considering that your dataframe is named df, use pandas.DataFrame.explode as shown below :

df['SubjectId'] = df['SubjectId'].str.strip('[]').str.split(',')

out = df.explode('SubjectId')

Note : Remove the first line if the values of the column SubjectId are 'list'.

print(out)

   QuestionId  User Id  Answerl Id  IsCorrect  CorrectAnswer  AnswerValue SubjectId
0         898     2111      280203          1              2            2         3
0         898     2111      280203          1              2            2        49
0         898     2111      280203          1              2            2        62
0         898     2111      280203          1              2            2        70
1         898     4884     1355100          0              2            4         3
1         898     4884     1355100          0              2            4        49
1         898     4884     1355100          0              2            4        62
1         898     4884     1355100          0              2            4        70
2         898     3131      481614          0              2            4         3
2         898     3131      481614          0              2            4        49
2         898     3131      481614          0              2            4        62
Related