I have a script the read a directory and processes its data into a pandas dataframe. I…
- User
I have a script the read a directory and processes its data into a pandas dataframe. I will paste it below.
I have a similar task now, with data in a different format, the final data has to fit in the same dataframe. We will walk through it together.
Here is the initial script: """ def load_annotated_jsons(root_dir): rows = []
for dirpath, _, filenames in os.walk(root_dir): for file in filenames: if file.endswith('.json'): file_path = os.path.join(dirpath, file) rel_path = os.path.relpath(file_path, root_dir) path_parts = rel_path.split(os.sep) try: with open(file_path, 'r', encoding='utf-8') as f: data = json.load(f)[0] # Extract metadata discussion = data.get("Discussion") turn = int(data.get("Turn")) hierarchy = int(data.get("Hierarchy")) author = data.get("Author") timestamp = data.get("Timestamp") annotations = data.get("Annotation", []) # One row per annotation entry for ann in annotations: row = { "Discussion": discussion, "Turn": turn, "Hierarchy": hierarchy, "Author": author, "Timestamp": timestamp, "DU_id": ann.get("DU_id"), "eTU_id": ann.get("eTU_id"), "eTU_type": ann.get("eTU_type"), "Text": ann.get("Text"), "Text_str": " ".join(ann.get("Text", [])), "file_name": file, "full_path": rel_path, "path_parts": path_parts[:-1] } rows.append(row) except Exception as e: print(f"Failed to read {file_path}: {e}") df = pd.DataFrame(rows) # Expand folder levels if needed max_depth = df['path_parts'].map(len).max() if not df.empty else 0 for i in range(max_depth): df[f'folder_level_{i + 1}'] = df['path_parts'].apply(lambda x: x[i] if i < len(x) else None) return df.drop(columns=['path_parts'])if name == 'main': json_dir = "/home/esther/Documents/phd/kamin_proj_data/HUJI BIU Kamin Project Data 2022_2024-20250526T081822Z-1-001/HUJI BIU Kamin Project Data 2022_2024/3. Annotated Data Repository (JSON files)" all_data = load_annotated_jsons(json_dir) """
- Assistant
Thanks for sharing your script! It’s well-structured and clearly processes hierarchical JSON files, extracting both metadata and annotation details into a flat pandas.DataFrame. Recap of Your Script's Output Structure Your resulting DataFrame has the following columns:
Discussion
Turn
Hierarchy
Author
Timestamp
DU_id
eTU_id
eTU_type
Text (a list)
Text_str (a space-joined string of Text)
file_name
full_path
folder_level_1, folder_level_2, ..., etc. (based on path depth)
Now, you mentioned:
"I have a similar task now, with data in a different format, the final data has to fit in the same dataframe."
Great! To proceed smoothly, please do the following: What I Need from You Next
Describe or paste a sample of the new data format you're dealing with. A sample JSON or other structure would be ideal.
Let me know:
Are the same fields (Discussion, Turn, Hierarchy, etc.) still present but stored differently?
Or are they embedded in a different nesting or naming structure?
- User
- Assistant
Great — thanks for the thorough explanation and for uploading the example noab.tsv file. Let’s take this step by step.
🧭 Your Goal: You want to read and process these TSV annotation files from a nested directory structure and produce a DataFrame that matches the format of your original JSON-based DataFrame (from your first script).
✅ Next Steps Now that I have the uploaded noab.tsv, I'll inspect its structure and show how we can:
Parse it.
Extract relevant annotation information.
Align it with your original column format: arduinoCopyEdit"Discussion", "Turn", "Hierarchy", "Author", "Timestamp", "DU_id", "eTU_id", "eTU_type", "Text", "Text_str", "file_name", "full_path", "folder_level_X"
Let me take a look at the file now.
- User
- Assistant
It looks like there was a temporary issue accessing the uploaded file. Could you please re-upload the noab.tsv file? Once I have it, I’ll read and analyze its structure so we can start building the new loader function.
- User
I want a funtion that reads all the tsv files in the parent directory and parses them into a pandas datafame.
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.