Method Chaining - Problem
You're working as a wildlife researcher analyzing animal data! Given a DataFrame containing information about various animals, your task is to identify the heavyweight champions of the animal kingdom.
Your mission: Find all animals that weigh strictly more than 100 kilograms and return their names sorted by weight in descending order (heaviest first).
DataFrame Schema:
| Column Name | Type | Description |
|---|---|---|
name | object | Animal's name |
species | object | Animal species |
age | int | Animal's age in years |
weight | int | Animal's weight in kilograms |
This problem demonstrates method chaining - a powerful technique where you chain multiple DataFrame operations together for clean, readable data manipulation code.
Input & Output
example_1.py โ Basic Case
$
Input:
DataFrame:
| name | species | age | weight |
|----------|---------|-----|--------|
| Tatiana | Snake | 98 | 464 |
| Khaled | Giraffe | 50 | 41 |
| Alex | Leopard | 6 | 328 |
| Jonathan | Monkey | 45 | 463 |
| Stefan | Bear | 100 | 50 |
| Tommy | Panda | 26 | 349 |
โบ
Output:
| name |
|----------|
| Tatiana |
| Jonathan |
| Tommy |
| Alex |
๐ก Note:
Animals with weight > 100kg: Tatiana(464kg), Alex(328kg), Jonathan(463kg), Tommy(349kg). Sorted by weight descending: Tatiana(464), Jonathan(463), Tommy(349), Alex(328).
example_2.py โ All Heavy Animals
$
Input:
DataFrame:
| name | species | age | weight |
|---------|----------|-----|--------|
| Jumbo | Elephant | 25 | 5000 |
| Rhino | Rhino | 15 | 2300 |
| Hippo | Hippo | 8 | 1500 |
โบ
Output:
| name |
|-------|
| Jumbo |
| Rhino |
| Hippo |
๐ก Note:
All animals weigh more than 100kg. Sorted by weight: Jumbo(5000kg) > Rhino(2300kg) > Hippo(1500kg).
example_3.py โ No Heavy Animals
$
Input:
DataFrame:
| name | species | age | weight |
|--------|---------|-----|--------|
| Fluffy | Cat | 2 | 5 |
| Buddy | Dog | 5 | 30 |
| Tweet | Bird | 1 | 0.5 |
โบ
Output:
Empty DataFrame:
| name |
|------|
| |
๐ก Note:
No animals weigh more than 100kg, so the result is an empty DataFrame with just the 'name' column.
Visualization
Tap to expand
Understanding the Visualization
1
Input Gate
All animal records enter the processing pipeline
2
Weight Scanner
Automatic filter removes animals โค 100kg
3
Sorting Scale
Remaining animals sorted by weight (heaviest first)
4
Name Tag Printer
Only animal names are output for the final list
Key Takeaway
๐ฏ Key Insight: Method chaining creates an elegant, readable pipeline that leverages pandas' optimized C implementations for maximum performance.
Time & Space Complexity
Time Complexity
O(n log n)
O(n) for filtering + O(n log n) for sorting, but optimized at C level
โก Linearithmic
Space Complexity
O(k)
O(k) for the filtered result where k โค n
โ Linear Space
Constraints
- 1 โค animals.length โค 104
- Animal weights are non-negative integers
- Weight threshold is strictly greater than 100kg (not inclusive)
- Animal names are unique within the dataset
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code