This repository was archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCleanData.py
More file actions
58 lines (44 loc) · 2.18 KB
/
CleanData.py
File metadata and controls
58 lines (44 loc) · 2.18 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
import pandas as pd
import re
def CleanData(file_path):
# Reading the file
# Load the data from the CSV file
data = pd.read_csv(file_path)
# Removing the specified columns and reordering the 'Item Number – 8 digit' column
columns_to_remove = ['Item Number – 6 digit', 'UPC Number', 'Constant',
'Customer-Specific Item Number', 'Pack Size Divisor',
'RX/OTC Indicator']
# Removing the columns
data_cleaned = data.drop(columns=columns_to_remove)
# Reordering 'Item Number – 8 digit' to the left
column_to_move = data_cleaned.pop('Item Number – 8 digit')
data_cleaned.insert(0, 'Item Number – 8 digit', column_to_move)
# Moving all price columns and the contract flag to the right
columns_to_move = ['AWP Price', 'Acquisition Price', 'Retail Price', 'WAC Price', 'Contract Flag']
for col in columns_to_move:
data_cleaned[col] = data_cleaned.pop(col)
# Function to split the generic description into generic name and form
def split_description(desc):
match = re.search(r'[A-Z]', desc)
if match:
index = match.start()
return desc[:index].strip(), desc[index:].strip()
else:
return desc, ''
# Applying the function to split 'Generic Description'
data_cleaned['Generic Name'], data_cleaned['Form'] = zip(*data_cleaned['Generic Description'].apply(split_description))
data_cleaned.drop(columns=['Generic Description'], inplace=True)
# Removing rows where 'Generic Name' is empty or whitespace
data_cleaned = data_cleaned[data_cleaned['Generic Name'].str.strip() != '']
# Function to split the description into name and size
def split_description_on_number(desc):
match = re.search(r'\d', desc)
if match:
index = match.start()
return desc[:index].strip(), desc[index:].strip()
else:
return desc, ''
# Applying the function to split 'Description'
data_cleaned['Name'], data_cleaned['Size'] = zip(*data_cleaned['Description'].apply(split_description_on_number))
data_cleaned.drop(columns=['Description'], inplace=True)
return data_cleaned