-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path2_data_cleaning.py
61 lines (45 loc) · 1.53 KB
/
2_data_cleaning.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
49
50
51
52
53
54
55
56
57
58
59
60
61
import pandas as pd
import numpy as np
import os
df = pd.read_csv("./datasets/raw/abbvie.csv")
columns = list(df.columns)
def change_name_year(df, name, year_list, columns):
arr = df.to_numpy()
for row in range(len(arr)):
for col in range(len(arr[0])):
if col == 0:
arr[row][col] = name
if col == 1:
arr[row][col] = year_list[row]
return pd.DataFrame(arr, columns=columns)
# Returns combined dataset
def combine_dataset(PATH, raw_data_files, company_names_list, year_list):
for i, (file, company) in enumerate(zip(raw_data_files, company_names_list)):
df = pd.read_csv(PATH + file)
df = df.iloc[:, 1:]
# Setting up appropriate names and year
df = change_name_year(df, company, year_list, columns)
# Skipping concatenation in the first iteration
if i == 0:
arr = df.to_numpy()
else:
arr1 = df.to_numpy()
arr = np.concatenate((arr, arr1), axis=0)
return pd.DataFrame(arr, columns=columns)
raw_data_files = os.listdir("./datasets/raw/")
company_names_list = [
"Meta",
"Merck & Co.",
"Alphabet Inc.",
"Microsoft Corporation",
"Costco",
"Pfizer",
"PepsiCo",
"AbbVie",
"Coca-Cola",
"Mastercard",
]
PATH = './datasets/raw/'
year_list = ['2021','2020','2019','2018','2017']
final_combined_dataset = combine_dataset(PATH, raw_data_files, company_names_list, year_list)
final_combined_dataset.to_csv('./datasets/final_combined_dataset.csv')