Advertisement
GeorgiLukanov87

cars_task_pandas

Sep 29th, 2023 (edited)
796
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.71 KB | None | 0 0
  1. # main.py
  2. import pandas as pd
  3. import json
  4. from logger import setup_logger
  5.  
  6.  
  7. class CarDataAnalyzer:
  8.     def __init__(self, input_json_file, output_file):
  9.         self.input_json_file = input_json_file
  10.         self.output_file = output_file
  11.         self.data = None
  12.         self.logger = setup_logger()
  13.  
  14.     # Create method to read the input json file
  15.     def load_data(self):
  16.         try:
  17.             with open(self.input_json_file, 'r') as file:
  18.                 self.data = json.load(file)
  19.             self.logger.info(f"Loaded data from {self.input_json_file}")
  20.  
  21.         # Raise exception if json not found
  22.         except FileNotFoundError:
  23.             self.logger.error(f"File not found: {self.input_json_file}")
  24.             raise
  25.  
  26.         # Raise exception if something is wrong with the json file
  27.         except json.JSONDecodeError:
  28.             self.logger.error(f"JSON decoding error in {self.input_json_file}")
  29.             raise
  30.  
  31.     def analyze_data(self):
  32.         if self.data is None:
  33.             self.logger.error("No data loaded.")
  34.             return
  35.  
  36.         # Convert data to a DataFrame using pandas
  37.         df_car_data = pd.DataFrame(self.data)
  38.  
  39.         # Format the "Year" column to display only the year part
  40.         df_car_data['Year'] = pd.to_datetime(df_car_data['Year']).dt.year
  41.  
  42.         unique_cars = df_car_data['Name'].nunique()
  43.         average_hp = df_car_data['Horsepower'].mean()
  44.  
  45.         # Extract the top 5 heaviest cars with only "Name" and "Weight_in_lbs" columns
  46.         heaviest_cars = df_car_data.nlargest(5, 'Weight_in_lbs')[['Weight_in_lbs', 'Name']]
  47.         cars_by_manufacturer = df_car_data['Origin'].value_counts()
  48.  
  49.         # Count the number of cars by year and sort in reversed order
  50.         cars_by_year = df_car_data['Year'].value_counts().sort_index(ascending=True)
  51.  
  52.         # Print results
  53.         print('----------------------------------------------')
  54.         print(f"Number of Unique Cars: {unique_cars}")
  55.         print(f"Average Horsepower of All Cars: {average_hp:.2f}")
  56.         print('----------------------------------------------')
  57.         print("Top 5 Heaviest Cars (Name and Weight):\n")
  58.         print(heaviest_cars.to_string(index=False))
  59.         print('----------------------------------------------')
  60.         print("Number of Cars by Manufacturer:\n")
  61.         print(cars_by_manufacturer.to_string())
  62.         print('----------------------------------------------')
  63.         print("Number of Cars by Year:\n")
  64.         print(cars_by_year.to_string())
  65.  
  66.         # Save data frame to csv file
  67.         df_car_data.to_csv(self.output_file, index=False)
  68.         self.logger.info(f"Saved data to {self.output_file}")
  69.  
  70. ==================================================================================================
  71. # logger.py
  72. import logging
  73.  
  74.  
  75. # Create logger
  76. def setup_logger():
  77.     logger = logging.getLogger('CarDataAnalyzer')
  78.     logger.setLevel(logging.DEBUG)
  79.     formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  80.  
  81.     ch = logging.StreamHandler()
  82.     ch.setFormatter(formatter)
  83.     logger.addHandler(ch)
  84.  
  85.     return logger
  86.  
  87. ==================================================================================================
  88. # controller.py
  89. from main import CarDataAnalyzer
  90.  
  91. if __name__ == "__main__":
  92.     input_json_file = "cars.json"
  93.     output_result_file = "car_data_result_output.csv"
  94.  
  95.     analyzer = CarDataAnalyzer(input_json_file, output_result_file)
  96.  
  97.     try:
  98.         analyzer.load_data()
  99.         analyzer.analyze_data()
  100.  
  101.     except Exception as err:
  102.         print(f"An error occurred: {str(err)}")
  103.  
  104. ==================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement