Python - 如何将 JSON 文件转换为数据帧 [英] Python - How to convert JSON File to Dataframe
本文介绍了Python - 如何将 JSON 文件转换为数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将这样的 JSON 文件转换为数据帧以进行一些转换.
How can I convert a JSON File as such into a dataframe to do some transformations.
例如,如果 JSON 文件读取:
For Example if the JSON file reads:
{"FirstName":"John",
"LastName":"Mark",
"MiddleName":"Lewis",
"username":"johnlewis2",
"password":"2910"}
如何将其转换为这样的表格
How can I convert it to a table like such
Column -> FirstName | LastName | MiddleName | username | password
Row -----> John | Mark |Lewis | johnlewis2 |2910
推荐答案
从字典对象创建数据框.
Creating dataframe from dictionary object.
import pandas as pd
data = [{'name': 'vikash', 'age': 27}, {'name': 'Satyam', 'age': 14}]
df = pd.DataFrame.from_dict(data, orient='columns')
df
Out[4]:
age name
0 27 vikash
1 14 Satyam
如果你有嵌套的列,那么你首先需要规范化数据:
If you have nested columns then you first need to normalize the data:
data = [
{
'name': {
'first': 'vikash',
'last': 'singh'
},
'age': 27
},
{
'name': {
'first': 'satyam',
'last': 'singh'
},
'age': 14
}
]
df = pd.DataFrame.from_dict(pd.json_normalize(data), orient='columns')
df
Out[8]:
age name.first name.last
0 27 vikash singh
1 14 satyam singh
来源:
这篇关于Python - 如何将 JSON 文件转换为数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文