-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path@classmethod.py
More file actions
53 lines (49 loc) · 1.9 KB
/
@classmethod.py
File metadata and controls
53 lines (49 loc) · 1.9 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
import pandas as pd
class Car:
def __init__(self, make, model, year):
self.make, self.model, self.year = make, model, year
def record(self):
return f"{self.year} {self.make} {self.model}"
class DataAnalyzer:
def __init__(self, car_data):
self.car_data = car_data
def analyze(self):
print("Analyzing car data:")
for car in self.car_data:
description = car.record()
print(description)
@classmethod # Build a class-level method, such that we can call this method directly from the class DataAnalyzer, instead of instantiating an object from the class DataAnalyzer
def from_csv(cls, csv_path):
# Use Pandas to read data from the CSV file
df = pd.read_csv(csv_path)
# Extract data from the DataFrame and create Car objects
car_data = []
for index, row in df.iterrows():
make = row['Make']
model = row['Model']
year = row['Year']
car = Car(make, model, year)
car_data.append(car)
return cls(car_data)
@classmethod
def from_dataframe(cls, df):
car_data = []
for index, row in df.iterrows():
make = row['Make']
model = row['Model']
year = row['Year']
car = Car(make, model, year)
car_data.append(car)
return cls(car_data)
# Example usage:
# Assume we have a CSV file named "car_data.csv" with columns "Make", "Year", and "Model"
analyzer_1 = DataAnalyzer.from_csv("Desktop/car_data.csv") #! Class-level method
analyzer_1.analyze()
# Assume we have a pandas data frame names "car_data" at hand with columns "Make", "Model", and "Year"
car_data = pd.DataFrame({
'Make': ['Toyota', 'Honda', 'Ford'],
'Model': ['Camry', 'Civic', 'Fusion'],
'Year': ['2022', '2021', '2020']
})
analyzer_2 = DataAnalyzer.from_dataframe(car_data)
analyzer_2.analyze()