Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I convert this dictionary to a dataframe in python?

I have a dictionary of Student information in this format. I cannot change this, it is the output from another program I am trying to use.

student_info_dict = {
    "Student_1_Name": "Alice",
    "Student_1_Age": 23,
    "Student_1_Phone_Number": 1111,
    "Student_1_before_after": (120, 109),
    "Student_2_Name": "Bob",
    "Student_2_Age": 56,
    "Student_2_Phone_Number": 1234,
    "Student_2_before_after": (115, 107),
    "Student_3_Name": "Casie",
    "Student_3_Age": 47,
    "Student_3_Phone_Number": 4567,
    "Student_3_before_after": (180, 140),
    "Student_4_Name": "Donna",
    "Student_4_Age": 33,
    "Student_4_Phone_Number": 6789,
    "Student_4_before_after": (150, 138),
}

The keys to my dictionary increment by 1 to display the next students information. How do I convert this to a DataFrame that looks like this:

   Name   Age  Phone_Number Before_and_After
0  Alice  23   1111        (120,109)
1  Bob    56   1234        (115,107)
3  Casie  47   4567        (180,140)
4  Donna  33   6789        (150,138)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use:

#create Series
s = pd.Series(student_info_dict)
#split index created by keys to second _
s.index = s.index.str.split('_', n=2, expand=True)
#remove first level (Student) and reshape to DataFrame
df = s.droplevel(0).unstack()
print (df)
  Age   Name Phone_Number before_after
1  23  Alice         1111   (120, 109)
2  56    Bob         1234   (115, 107)
3  47  Casie         4567   (180, 140)
4  33  Donna         6789   (150, 138)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading