-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.py
More file actions
59 lines (43 loc) · 1.36 KB
/
script.py
File metadata and controls
59 lines (43 loc) · 1.36 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
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
import logging
matplotlib.use("Agg")
# configure logging
logging.basicConfig(
level=logging.WARN,
format="%(asctime)s - %(levelname)s - %(message)s",
handlers=[
logging.StreamHandler(), # log to the console
logging.FileHandler("log.txt"),
],
)
def extract(file_path):
logging.info(f"Attempting to read data from {file_path}")
try:
data = pd.read_csv(file_path)
logging.info(f"Successfully read the data.")
return data
except Exception as e:
logging.error(f"Error reading file: {e}")
def transform(data):
data["Salary_USD"] = data["Salary_GBP"] * 1.25
data["Years_to_Retirement"] = data["Age"].apply(lambda x: max(0, 65 - x))
return data
def create_plot(data):
plt.figure(figsize=(10, 6))
plt.scatter(data["Age"], data["Salary_USD"], alpha=0.7, edgecolors="k")
plt.title("Age vs Salary (USD)", fontsize=16)
plt.xlabel("Age", fontsize=14)
plt.ylabel("Salary (USD)", fontsize=14)
plt.grid(True, linestyle="--", alpha=0.6)
plt.tight_layout()
plt.savefig("age_salary.png")
def main():
file_path = "input.csv"
data = extract(file_path)
transformed_data = transform(data)
create_plot(transformed_data)
logging.debug(transformed_data)
if __name__ == "__main__":
main()