File size: 2,039 Bytes
ef6fa9a
 
a90a6d5
ef6fa9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
# from backend.population_pandas import get_continents, get_continent_data
from logger import logger

import os

# file_path = os.path.join(os.path.dirname(__file__), "../../data/world_population.csv")
file_path = os.path.join(os.path.dirname(__file__), "../data/world_population.csv")
# file_path = os.path.abspath(file_path)  # Convert to absolute path

file_path = os.path.abspath(file_path)  # Convert to absolute path
try:
    df = pd.read_csv(file_path)
    logger.info(f"CSV file loaded successfully from: {file_path}")
except Exception as e:
    logger.error(f"Error loading CSV file from {file_path}: {e}")
    df = None  # Prevent NameError if file loading fails

if df is not None:
    # Perform the aggregations only if df is successfully loaded
    continent_stats = df.groupby("Continent").agg(
        Total_Countries=('Country', 'count'),
        Total_Population=('Population', 'sum'),
        Average_Population=('Population', 'mean'),
        Total_Area=('Area', 'sum'),
        max_population=('Population', 'max'),
        min_population=('Population', 'min'),
        Country_Max_Population=('Population', lambda x: df.loc[x.idxmax(), 'Country']),
        Country_Min_Population=('Population', lambda x: df.loc[x.idxmin(), 'Country'])
    ).reset_index()

    # Compute Population Density
    continent_stats["Population_Density"] = (
        continent_stats["Total_Population"] / continent_stats["Total_Area"]
    )

    logger.info("Data processing completed.")

def get_continents():
    """Returns a list of all continents."""
    logger.info("Fetching all continents.")
    return continent_stats["Continent"].tolist()

def get_continent_data(continent):
    """Returns statistics for a specific continent."""
    logger.info(f"Fetching data for continent: {continent}")
    result = continent_stats[continent_stats["Continent"] == continent].squeeze()

    if result.empty:
        logger.warning(f"No data found for continent: {continent}")
        return {}

    return result.to_dict()