将dict列表转换为pandas dataframe
作者:互联网
import json
import pandas as pd
dic1 = {'name': '小刘', 'age': 23}
dic2 = {'name': '小王', 'age': 24}
dic3 = {'name': '小张', 'age': 25}
dic4 = {'name': '小李', 'age': 26}
lst = [dic1, dic2, dic3, dic4]
print("lst", type(lst), lst)
# 将list转化为json字符串
jn = json.dumps(lst)
print("jn", type(jn), jn)
# pandas读取json字符串,转化为Dataframe
df = pd.read_json(jn)
print("df", type(df), df)
结果
lst <class 'list'> [{'name': '小刘', 'age': 23}, {'name': '小王', 'age': 24}, {'name': '小张', 'age': 25}, {'name': '小李', 'age': 26}]
jn <class 'str'> [{"name": "\u5c0f\u5218", "age": 23}, {"name": "\u5c0f\u738b", "age": 24}, {"name": "\u5c0f\u5f20", "age": 25}, {"name": "\u5c0f\u674e", "age": 26}]
df <class 'pandas.core.frame.DataFrame'> name age
0 小刘 23
1 小王 24
2 小张 25
3 小李 26
标签:name,df,age,json,jn,dataframe,lst,dict,pandas 来源: https://blog.csdn.net/yuer5531/article/details/114656934