1
{'student1': 45,
 'student2': 78,
 'student3': 12,
 'student4': 14,
 'student5': 48,
 'student6': 43,
 'student7': 47,
 'student8': 98,
 'student9': 35,
 'student10': 80}

How to convert this dict into a dataframe

Dyno Fu
  • 8,753
  • 4
  • 39
  • 64
tachyon
  • 47
  • 1
  • 6

4 Answers4

1
import pandas as pd

student = {
    "student1": 45,
    "student2": 78,
    "student3": 12,
    "student4": 14,
    "student5": 48,
    "student6": 43,
    "student7": 47,
    "student8": 98,
    "student9": 35,
    "student10": 80,
}

df = pd.DataFrame(student.items(), columns=["name", "score"])
print(df)
        name  score
0   student1     45
1   student2     78
2   student3     12
3   student4     14
4   student5     48
5   student6     43
6   student7     47
7   student8     98
8   student9     35
9  student10     80
Dyno Fu
  • 8,753
  • 4
  • 39
  • 64
0
import pandas as pd 

# intialise data of lists. where each key will be your column 
data = {'Name':['Tom', 'nick', 'krish', 'jack'], 'Age':[20, 21, 19, 18]} 

# Create DataFrame 
df = pd.DataFrame(data) 

# or list of dicts
data = [{'a': 1, 'b': 2, 'c':3}, {'a':10, 'b': 20, 'c': 30}] 

if you are getting scalar error

do this

import pandas as pd

data = {'student1': 45, 'student2': 78, 'student3': 12, 'student4': 14, 'student5': 48, 'student6': 43, 'student7': 47, 'student8': 98, 'student9': 35, 'student10': 80}
for i in data.keys():
    data[i] = [data[i]]
df = pd.DataFrame(data)
df.head()
Kuldeep Singh Sidhu
  • 3,748
  • 2
  • 12
  • 22
0

This should do the trick

df = DataFrame(list(my_dict.items()),columns = ['column1','column2'])
Shreamy
  • 341
  • 2
  • 16
0
pd.DataFrame(dict_.items())
pd.DataFrame(dict_.items(), columns=['Student', 'Point'])
pd.Series(dict_, name='StudentValue')

All will work.