forked from ZCW-Spring26/CentralLibraryData
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreader.py
More file actions
57 lines (44 loc) · 1.65 KB
/
Copy pathreader.py
File metadata and controls
57 lines (44 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import pandas as pd
import glob
import os
# 1. Create a new folder for the cleaned data
output_dir = "cleaned_data"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Define your required columns
required_keys = ['id', 'user_name', 'email']
# Identify all target files
csv_files = glob.glob("*.csv")
json_files = glob.glob("data/*.json")
all_files = csv_files + json_files
dataframes = []
for file in all_files:
# Load data based on extension
if file.endswith('.csv'):
df = pd.read_csv(file)
else:
df = pd.read_json(file)
# Ensure required columns exist
for col in required_keys:
if col not in df.columns:
df[col] = None
# 2. Report missing values (blanks)
missing_counts = df.isnull().sum()
missing_only = missing_counts[missing_counts > 0]
if not missing_only.empty:
print(f"--- Missing data in {file} ---")
print(missing_only)
# 3. Fill all blanks with null (pd.NA)
df = df.fillna(value=pd.NA)
dataframes.append(df)
# 4. Combine, filter duplicates, and save
if dataframes:
df_combined = pd.concat(dataframes, ignore_index=True)
# Drop duplicates (keeping the first occurrence)
df_cleaned = df_combined.drop_duplicates().reset_index(drop=True)
# Save to JSON in the new folder
output_path = os.path.join(output_dir, "master_cleaned_data.json")
# 'records' format makes it a clean list of objects
df_cleaned.to_json(output_path, orient='records', indent=4)
print(f"\nSuccess! Filtered data saved to: {output_path}")
print(f"Original rows: {len(df_combined)} | Rows after filtering: {len(df_cleaned)}")