将字典转换为数据框?

时间:2020-04-24 05:54:13

标签: python

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

如何将此字典转换为数据框

4 个答案:

答案 0 :(得分: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

答案 1 :(得分: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}] 

如果出现标量错误

执行此操作

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()

答案 2 :(得分:0)

这应该可以解决问题

df = DataFrame(list(my_dict.items()),columns = ['column1','column2'])

答案 3 :(得分:0)

pd.DataFrame(dict_.items())
pd.DataFrame(dict_.items(), columns=['Student', 'Point'])
pd.Series(dict_, name='StudentValue')

一切正常。