-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_processor.py
More file actions
69 lines (48 loc) · 1.54 KB
/
Copy pathdata_processor.py
File metadata and controls
69 lines (48 loc) · 1.54 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
60
61
62
63
64
65
66
67
68
69
import pandas as pd
import re
def standardize_columns(df):
df.columns = df.columns.str.lower().str.strip()
return df
def clean_description(text):
text = str(text).capitalize()
# Remove currency words
text = re.sub(r'\b(pkr|usd|rs)\b', '', text)
# Remove numbers
text = re.sub(r'\d+', '', text)
# Remove special characters
text = re.sub(r'[^a-zA-Z\s]', ' ', text)
# Remove extra spaces
text = re.sub(r'\s+', ' ', text).strip()
return text
def process_csv(df):
df = standardize_columns(df)
required_cols = ["date", "spent_on", "importance", "amount"]
for col in required_cols:
if col not in df.columns:
raise ValueError(
"CSV must contain: date, spent_on, importance, amount"
)
# Parse date
df["date"] = pd.to_datetime(df["date"], errors="coerce")
# Clean description properly
df["spent_on"] = df["spent_on"].apply(clean_description)
# Normalize importance
df["importance"] = (
df["importance"]
.astype(str)
.str.lower()
.str.strip()
)
# Standardize importance categories
importance_map = {
"optional": "Optional",
"survival": "Essential",
"obligation": "Essential",
"investment": "Investment"
}
df["importance"] = df["importance"].map(importance_map).fillna("Optional")
# Ensure numeric amount
df["amount"] = pd.to_numeric(df["amount"], errors="coerce")
# Drop invalid rows
df = df.dropna(subset=["date", "amount"])
return df