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]}]
Mike 98 86 90 72
Sasha 92 79 85 94
Beth 89 89 76 90
for i in ListOfGrades:
ListOfGrades[i]=str(ListOfGrades[i]['grades'])
# Convert to dataframe
df = pd.DataFrame.from_dict(ListOfGrades, orient='index').reset_index()
ListOfGrades[i]=str(ListOfGrades[i]['grades'])
TypeError: list indices must be integers, not dict
Ok, this approach is a bit of a hack, and it will quickly run into problems if each student doesn't have the same number of grades, but essentially, you need to build a new list and create the dictionary from that list. For python 3.5:
new_list = []
for student in ListOfGrades:
new_list.append({'Name': student['Name'], **{'grade_'+str(i+1): grade for i, grade in enumerate(student['grades'])}})
df = pd.DataFrame(new_list)
This is the dataframe I'm getting:
Name grade_1 grade_2 grade_3 grade_4
0 Mike 98 86 90 72
1 Sasha 92 79 85 94
2 Beth 89 89 76 90
If you don't have python 3.5 but have a version of python 3, this should work:
new_list = []
for student in ListOfGrades:
new_list.append(dict(Name = student['Name'], **{'grade_'+str(i+1): grade for i, grade in enumerate(student['grades'])}))
df = pd.DataFrame(new_list)
Edited to add: The above should also work for python 2.7