-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess_data.py
48 lines (37 loc) · 1.6 KB
/
preprocess_data.py
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
import pandas as pd
from sklearn.preprocessing import StandardScaler
def clean_data(data_frames):
for name, df in data_frames.items():
# Check for missing values
print(f"\nMissing values in {name}:")
print(df.isnull().sum())
# Drop rows or columns with excessive missing values
df_cleaned = df.dropna() # This is a simple approach; you might want to be more selective
# Check data types
print(f"\nData types in {name}:")
print(df.dtypes)
# Store cleaned dataframe back to the dictionary
data_frames[name] = df_cleaned
return data_frames
def normalize_data(data_frames):
for name, df in data_frames.items():
# Select only numeric columns for normalization
numeric_columns = df.select_dtypes(include=[float, int]).columns
if not df.empty and not numeric_columns.empty:
scaler = StandardScaler()
df_scaled = df.copy()
df_scaled[numeric_columns] = scaler.fit_transform(df[numeric_columns])
# Store normalized dataframe back to the dictionary
data_frames[name] = df_scaled
return data_frames
if __name__ == "__main__":
# Example usage:
from load_data import load_all
import os
# Load all full data files and series family metadata files
data_frames = load_all()
# Clean and normalize the data
data_frames = clean_data(data_frames)
data_frames = normalize_data(data_frames)
# Print the processed dataframes' keys to see what has been processed
print("Processed dataframes:", data_frames.keys())