I have a problem writing the code that will read multiple json files from a folder in Python.
My json file example (file name: 20191111.json ) is like this:
[
{
"info1": {
"name": "John",
"age" : "50"
"country": "USA",
},
"info2": {
"id1": "129",
"id2": "151",
"id3": "196",
},
"region": [
{
"id": "36",
"name": "Spook",
"spot": "2"
},
{
"id": "11",
"name": "Ghoul",
"spot": "6"
},
{
"id": "95",
"lat": "Devil",
"spot": "4"
}
]
}
{
"info1": {
"name": "Mark",
"age" : "33"
"country": "Brasil",
},
"info2": {
"id1": "612",
"id2": "221",
"id3": "850",
},
"region": [
{
"id": "68",
"name": "Ghost",
"spot": "7"
},
{
"id": "75",
"name": "Spectrum",
"spot": "2"
},
{
"id": "53",
"name": "Phantom",
"spot": "2"
}
]
}
]
My code:
path = 'my_files_directory'
json_files = [pos_json for pos_json in os.listdir(path) if pos_json.endswith('.json')]
df = pd.DataFrame()
for file_ in json_files:
file_df = pd.read_json(file_ )
file_df['date'] = file_
df = df.append(file_df)
df = df.reset_index(drop=True)
Output:
info1 info2 region date
0 {'name': 'John', ...} {'id1': '129', ...} [{'id':'36','name':'Spook'... 20191111.json
1 {'name': 'Mark', ...} {'id1': '61', ...} [{'id':'36','name':'Ghost'... 20191111.json
Now I delete the first and second column because there is information that I don't need. Then I want to extract 'name' information from 'region' column
My code is:
df = df.drop(df.columns[[0,1]], axis=1)
df['name'] = [x[0]['name'] for x in df['region']]
Output:
name date
0 Spook 20191111.json
1 Ghost 20191111.json
But I'd like the corresponding DataFrame to look like this:
name date
0 Spook 20191111.json
1 Ghoul 20191111.json
2 Devil 20191111.json
3 Ghost 20191111.json
4 Spectrum 20191111.json
5 Phantom 20191111.json
What I have to do to get it? Thank you for your help.
df = df.append(file_df)in a loop is an anti-pattern. Instead, put all thefile_dfs into a list, and then dopd.concat()on that entire list at the end. Much faster.