mercredi 15 juin 2016

Import feature vectors from list of dictionaries into data frame python

I have a list of dictionaries, and each dictionary consists of two key-value tuples. The first key-value is the name of a person and the second one is a feature vector consisting of the grades each person achieved in different courses. For example:

ListOfGrades=[{'Name':"Mike", 'grades':[98,86,90,72]},{'Name':"Sasha", 'grades':[92,79,85,94]},{'Name':"Beth", 'grades':[89,89,76,90]}] 

I want to import this data into a pandas dataframe such that each row has the label of a person's name with each column filled with their grades. In short, I need to get something like this:

Mike    98  86  90  72
Sasha   92  79  85  94
Beth    89  89  76  90

I know I should use pd.DataFrame(ListOfGrades), but I'm not sure how to set it for my purpose. I have seen Convert list of dictionaries to Dataframe, but it's different from the way I want to order my data in the data frame. I have tried this:

for i in ListOfGrades:
    ListOfGrades[i]=str(ListOfGrades[i]['grades'])

# Convert to dataframe
df = pd.DataFrame.from_dict(ListOfGrades, orient='index').reset_index()

But, python throws me an error:

 ListOfGrades[i]=str(ListOfGrades[i]['grades'])
 TypeError: list indices must be integers, not dict 

Also, I don't know how to add the names to each row, such that the first column of my data frame consists of the name of people, like the way I want my data frame look (as I showed above). Any help is appreciated!

Aucun commentaire:

Enregistrer un commentaire