0
   name        age  is_male     marks                                   total_marks   
0   student1    23  True    {'science': 95, 'maths': 89, 'english': 95} 279
1   student2    30  False   {'science': 56, 'maths': 32, 'english': 67} 155
2   student3    25  True    {'science': 89, 'maths': 56, 'english': 99} 244
3   student4    28  False   {'science': 78, 'maths': 55, 'english': 98} 230

i want to convert the marks column values into columns which is in the dict format how can i convert to marks values to dataframe into different columns

    name       age  is_male science  maths  english  total_marks
0   student1    23  True    95        89    95        279
1   student2    30  False   56        32    67        155
2   student3    25  True    89        56    99        244
3   student4    28  False   78        55    98        230
0x5453
  • 12,753
  • 1
  • 32
  • 61
  • Please post a failing script that we can use to write an answer. – tdelaney May 13 '22 at 20:52
  • I will probably create a `dict` with the same keys and gather all the values into a list for each key. Then create a data frame from that big dict we just created. From there, append the new df with the original df by column. – Vae Jiang May 13 '22 at 21:01
  • Does this answer your question? [Python: Pandas dataframe from Series of dict](https://stackoverflow.com/questions/29681906/python-pandas-dataframe-from-series-of-dict) – ljmc May 13 '22 at 21:12
  • Use `json_normalize` – mozway May 13 '22 at 21:16
  • while question is closed it might be that your marks is actually not a dictionary but a string that hasnt been casted to dictionary. you can check with ``` type(df['marks'][0]) ```` if this returns dict type suggested method will work.... but if not you would have to have to convert to dict first and then apply suggested method – Yev Guyduy May 13 '22 at 21:33

0 Answers0