Zoological Climate Analysis
Beginner Mode

Start your terminal to use beginner mode.

Objective

In this problem, we are given two DataFrames: AnimalData and RegionData. The goal is to analyze the characteristics of various animal species across different regional climates.

Task

Write a PySpark function that joins the two DataFrames and groups the data by Species and Climate. Provide the average age, the average weight, and the total count of the animals for each species in each climate.

Note the AvgWeight column in the expected output. When calculating the average weight, you must truncate the decimal to turn it into a whole number (e.g., an average of 152.7 becomes 152).

Save your resulting DataFrame as result_df. Ensure the output matches the exact schema order requested and order the final DataFrame alphabetically by Species (ascending), and then by Climate (ascending).

File Path

  • Animal Dataset: /home/interview/animal_data.csv
  • Region Dataset: /home/interview/region_data.csv
  • Starter script: /home/interview/zoology.py

Schema

animal_data.csv

Column Name Type
ID String
Species String
Age Integer
Weight Float
Region String

region_data.csv

Column Name Type
Region String
Climate String

Expected Output Schema

Column Name Type
Species String
Climate String
AvgAge Float
AvgWeight Integer
TotalAnimals Integer

Example

Given this sample input:

AnimalData

| ID | Species | Age | Weight | Region |
|----+---------+-----+--------+---------------|
| A1 | Lion | 10 | 200.5 | Africa |
| A2 | Tiger | 5 | 150.3 | Asia |
| A3 | Bear | 7 | 180.2 | North America |
| A4 | Lion | 12 | 205.7 | Africa |
| A5 | Tiger | 6 | 155.1 | Asia |

RegionData

| Region | Climate |
|---------------+-----------|
| Africa | Hot |
| Asia | Temperate |
| North America | Cold |

The expected output would be:

| Species | Climate | AvgAge | AvgWeight | TotalAnimals |
|---------+-----------+--------+-----------+--------------|
| Bear | Cold | 7.0 | 180 | 1 |
| Lion | Hot | 11.0 | 203 | 2 |
| Tiger | Temperate | 5.5 | 152 | 2 |

Terminal requires a larger screen

Open this page on a desktop or tablet (≥ 768px) to launch the terminal and practice hands-on.

Linux Terminal Environment

Write and execute your solution in the terminal below.

Sign In

Track

Question Difficulty Company Access
Need more practice in this area? Explore more questions →