Load nested json with pandas read nested json with pandas
Google Corab
With pandas
import pandas as pd
pd.read_json('file.json')
When you do
index | title | content |
---|---|---|
1 | aaa | {"col1": "a", "col2": "aa"... |
2 | bbb | {"col1": "b", "col2": "bb"... |
Suppose you have a json file that looks like this: (It is a form in which json format character strings are nested and stored in content.)
this,
col1 | col2 |
---|---|
a | aa |
b | bb |
I wanted to read it.
I searched a little with normalize or flatten and found that pd.json_normalize ('file.json')
looked like that, so when I tried using it, I got angry with ʻAttribute Error:'str' object has no attribute'values'`. I did.
After trial and error,
import pandas as pd
df = pd.read_json('file.json')
df = pd.DataFrame(df.content.to_list())
I read it correctly in, so I will write it as an article.
Recommended Posts