-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_loader.py
More file actions
44 lines (31 loc) · 1.09 KB
/
data_loader.py
File metadata and controls
44 lines (31 loc) · 1.09 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
import os
import pandas as pd
def load_data(input_path):
if not os.path.exists(input_path):
raise FileNotFoundError(f"{input_path} not found")
# Single file
if os.path.isfile(input_path):
df = pd.read_csv(input_path)
return df, {"single_case": df}
# Folder
elif os.path.isdir(input_path):
all_files = [
os.path.join(input_path, f)
for f in os.listdir(input_path)
if f.endswith(".csv")
]
if not all_files:
raise ValueError("No CSV files found in directory")
df_list = []
individual_dfs = {}
for file in all_files:
temp_df = pd.read_csv(file)
case_name = os.path.splitext(os.path.basename(file))[0]
if "case_name" not in temp_df.columns:
temp_df["case_name"] = case_name
df_list.append(temp_df)
individual_dfs[case_name] = temp_df
combined_df = pd.concat(df_list, ignore_index=True)
return combined_df, individual_dfs
else:
raise ValueError("Invalid input path")