Last active
July 2, 2020 00:51
-
-
Save notionparallax/6f63783d9cb008eac8bf98f68af254e4 to your computer and use it in GitHub Desktop.
This is how I'd read a folder full of json files and turn them into a dataframe. There's probably a more efficient way that Pandas can do, but this gives a good level of fine control.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "A", | |
"chats": [ | |
{ | |
"dt":"2020-07-02 10:32:54.876422", | |
"from":"me", | |
"message":"yo" | |
},{ | |
"dt":"2020-07-02 10:35:54.876422", | |
"from":"them", | |
"message":"πβ" | |
},{ | |
"dt":"2020-07-02 10:36:54.876422", | |
"from":"me", | |
"message":"no" | |
} | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "B", | |
"chats": [ | |
{ | |
"dt":"2020-07-02 10:32:54.876422", | |
"from":"me", | |
"message":"yo" | |
},{ | |
"dt":"2020-07-02 10:35:54.876422", | |
"from":"them", | |
"message":"π³π₯ͺ" | |
},{ | |
"dt":"2020-07-02 10:36:54.876422", | |
"from":"me", | |
"message":"π yes π" | |
} | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
import pandas as pd | |
dataframes_of_chats = [] | |
for json_file in os.listdir("."): | |
print(json_file) | |
if ".json" in json_file: | |
the_file_object = open(json_file, "r", encoding="utf-8") | |
file_contents = json.load(the_file_object) | |
print(file_contents) | |
messages = file_contents.get("chats") | |
correspondent = file_contents.get("name") | |
chats_with_x = pd.DataFrame(messages) | |
chats_with_x["correspondent"] = correspondent | |
dataframes_of_chats.append(chats_with_x) | |
all_the_chats = pd.concat(dataframes_of_chats) | |
print(all_the_chats) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment