Repository navigation
Expand file tree
/
Copy pathaggregate_duplicates_v2.py
More file actions
59 lines (48 loc) · 2.12 KB
/
Copy pathaggregate_duplicates_v2.py
File metadata and controls
59 lines (48 loc) · 2.12 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
58
59
import pandas as pd
import numpy as np
import os
# Set paths
input_path = 'master_aadhaar_data_final_cleaned.csv'
output_path = 'master_aadhaar_data_fully_cleaned.csv'
try:
print(f"Loading {input_path}...")
df = pd.read_csv(input_path)
initial_rows = len(df)
print(f"Initial row count: {initial_rows:,}")
# 1. Drop exact duplicates
df = df.drop_duplicates()
after_exact_drop = len(df)
print(f"Rows after dropping exact duplicates: {after_exact_drop:,} (Removed: {initial_rows - after_exact_drop:,})")
# 2. Define aggregation logic
key_columns = ['date', 'state', 'district', 'pincode', 'month_name', 'day_name', 'is_weekend']
# List all numeric columns to be summed
count_columns = [
'age_0_5', 'age_5_17', 'age_18_greater',
'bio_age_5_17', 'bio_age_18_greater',
'demo_age_5_17', 'demo_age_18_greater',
'total_enrolments', 'total_biometric_updates',
'total_demographic_updates', 'total_updates', 'overall_activity'
]
# 3. Aggregate key duplicates by summing count columns
print("Aggregating key-column duplicates...")
df_aggregated = df.groupby(key_columns)[count_columns].sum().reset_index()
# 4. Recalculate derived ratios
print("Recalculating update_to_enrolment_ratio...")
df_aggregated['update_to_enrolment_ratio'] = (
df_aggregated['total_updates'] / (df_aggregated['total_enrolments'] + 0.1)
)
final_rows = len(df_aggregated)
print(f"Final row count: {final_rows:,}")
print(f"Total rows removed/consolidated: {initial_rows - final_rows:,}")
# Verify uniqueness
duplicate_keys = df_aggregated.duplicated(subset=['date', 'state', 'district', 'pincode']).sum()
if duplicate_keys == 0:
print("✓ Success: All rows are now unique by date and pincode.")
else:
print(f"⚠️ Warning: Still found {duplicate_keys} duplicate keys!")
# 5. Save to a NEW file to avoid permission issues
print(f"Saving to {output_path}...")
df_aggregated.to_csv(output_path, index=False)
print(f"Done! Data saved to {output_path}")
except Exception as e:
print(f"Error occurred: {e}")