【发布时间】:2021-01-28 08:26:59
【问题描述】:
我正在尝试使用 Youtube 分析和报告 API 从此处Google Reference 下载我的 youtube 数据。我正在使用来自谷歌的 Python 代码来下载这个工作正常的数据。数据以JSON格式检索如下图。
rawdata.json -
{
"kind": "youtubeAnalytics#resultTable",
"columnHeaders": [
{
"name": "day",
"columnType": "DIMENSION",
"dataType": "STRING"
},
{
"name": "views",
"columnType": "METRIC",
"dataType": "INTEGER"
},
{
"name": "estimatedMinutesWatched",
"columnType": "METRIC",
"dataType": "INTEGER"
},
{
"name": "averageViewDuration",
"columnType": "METRIC",
"dataType": "INTEGER"
},
{
"name": "averageViewPercentage",
"columnType": "METRIC",
"dataType": "FLOAT"
},
{
"name": "subscribersGained",
"columnType": "METRIC",
"dataType": "INTEGER"
}
],
"rows": [
[
"2020-10-01",
445,
1068,
144,
38.78,
3
],
[
"2020-10-02",
406,
905,
133,
34.94,
2
],
[
"2020-10-03",
466,
1042,
134,
34.77,
6
],
[
"2020-10-04",
427,
1006,
141,
36.48,
3
],
[
"2020-10-05",
379,
964,
152,
39.48,
2
]
]
}
我正在尝试将此 JSON 转换为 CSV 格式,但到目前为止没有运气。
这是我用来规范化和创建数据框但没有运气的 python 代码。
import json
import pandas as pd
from pandas import json_normalize
with open('rawdata.json') as json_file:
data = json.load(json_file)
#print (data)
df = json_normalize(data['rows'])
print(df)
非常感谢任何帮助或指导。
编辑:Suraj S 的回答。这对我有用。
import json
import pandas as pd
from pandas import json_normalize
with open('rawdata.json') as json_file:
data = json.load(json_file)
#print (data)
df = pd.DataFrame(data['rows'],columns=['day','views','comments','likes','dislikes','estimatedMinutesWatched','averageViewDuration','subscribersGained'])
print(df)
【问题讨论】:
标签: python json pandas dataframe