Nested list of dictionary with nested list of dictionary into a Pandas dataframe
You can also use json_normalize
here:
df = pd.json_normalize(review_stat, 'books')
[out]
id isbn ... work_text_reviews_count average_rating
0 30278752 1594634025 ... 109912 3.92
1 34006942 1501173219 ... 75053 4.33
I believe a faster way without needing to append
dataframes is to "flatten" the lists, because the dictionary contains single-key books
which also contains one element. Therefore, it should easy to flatten into a single list which can be passed to pd.DataFrame
:
df = pd.DataFrame([x['books'][0] for x in review_stat])
Outputs:
id isbn ... work_text_reviews_count average_rating
0 30278752 1594634025 ... 109912 3.92
1 34006942 1501173219 ... 75053 4.33
If you key is always books
pd.concat([pd.DataFrame(i['books']) for i in review_stat])
id isbn isbn13 ratings_count reviews_count text_reviews_count work_ratings_count work_reviews_count work_text_reviews_count average_rating
0 30278752 1594634025 9781594634024 4832 8435 417 2081902 3313007 109912 3.92
0 34006942 1501173219 9781501173219 4373 10741 565 1005504 2142280 75053 4.33
You can always reset the index if you need