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

Time:06-20

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)

CodePudding user response:

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)

CodePudding user response:

You can use a simple dictionary comprehension feeding the Series constructor, getting the Student number and the field name as keys, then unstacking to DataFrame:

df = (pd.Series({tuple(k.split('_',2)[1:]): v
                for k,v in student_info_dict.items()})
        .unstack(1)
     )

output:

  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)

CodePudding user response:

use this code:

import pandas as pd
name=[]
age=[]
pn=[]
baa=[]
for i in student_info_dict.keys():
    if i.find('Name')>=0:
        name.append(i)
    elif i.find('Age')>=0:
        age.append(i)
    elif i.find('Phone')>=0:
        pn.append(i)
    elif i.find('before')>=0:
        baa.append(i)
df = pd.DataFrame('Name':name, 'Age':age, 'Phone_number':pn, 'before_after':baa)

  • Related