Эти форматы json оказались вложенными, поэтому я обработал их следующим образом:
import pandas as pd
import os
x = {
"aggs": [
{
"cols": [
"depth",
"page_count"
],
"rows": [
[
1,
1
],
[
2,
661
],
[
3,
16773
],
[
4,
7078
],
[
5,
221
]
]
}
]
}
dfrows = []
dfcolumns = []
for y,z in x.items(): # x.items() is a nested dict with aggs is outer key x and z is list as the value of aggs:
for a in z: # a accesses the inner dict in the list
for j,k in a.items(): # key, value of rows and cols in inner dict
if j == 'rows':
dfrows.append(k) # make list of list of row values
if j == 'cols':
dfcolumns.append(k) # make list of list of column names
rows_flat_list = [item for x in dfrows for item in x] # flatten out list
columns_flat_list = [item for x in dfcolumns for item in x] # flatten out list
dfJson = pd.DataFrame(data = rows_flat_list, columns= columns_flat_list) # create df
dfJson.to_csv('./dfJson.csv', index=False) # write to csv
Выходной CSV-файл выглядит (или, если вы открываете в Excel, это формат Excelfile):
depth,page_count
1,1
2,661
3,16773
4,7078
5,221