Method Chaining - Problem

Given a DataFrame animals with columns name, species, age, and weight, write a solution to list the names of animals that weigh strictly more than 100 kilograms.

Return the animals sorted by weight in descending order.

Input & Output

Example 1 — Basic Filtering
$ Input: animals = [{"name":"Lion","species":"Panthera leo","age":5,"weight":80},{"name":"Elephant","species":"Loxodonta africana","age":10,"weight":150}]
Output: ["Elephant"]
💡 Note: Only Elephant weighs more than 100kg (150 > 100), so return ["Elephant"]
Example 2 — Multiple Heavy Animals
$ Input: animals = [{"name":"Rhino","species":"Rhinoceros","age":8,"weight":120},{"name":"Hippo","species":"Hippopotamus","age":6,"weight":180},{"name":"Elephant","species":"Elephantidae","age":12,"weight":150}]
Output: ["Hippo","Elephant","Rhino"]
💡 Note: All three animals weigh > 100kg. Sorted by weight descending: Hippo(180), Elephant(150), Rhino(120)
Example 3 — No Heavy Animals
$ Input: animals = [{"name":"Tiger","species":"Panthera tigris","age":4,"weight":90},{"name":"Lion","species":"Panthera leo","age":3,"weight":85}]
Output: []
💡 Note: No animals weigh more than 100kg (Tiger: 90kg, Lion: 85kg), so return empty array

Constraints

  • 1 ≤ animals.length ≤ 104
  • 1 ≤ weight ≤ 1000
  • name contains only letters and spaces

Visualization

Tap to expand
Method Chaining - DataFrame Filtering INPUT name species age weight Lion P. leo 5 80 Elephant L. afric. 10 150 Condition: weight > 100 kg Weight Check: Lion: 80 kg Elephant: 150 kg 100kg DataFrame: animals ALGORITHM STEPS 1 Filter by Weight df[df['weight'] > 100] 2 Sort Descending .sort_values('weight', ascending=False) 3 Select Names [['name']] 4 Return Result Output filtered names animals .query('weight > 100') .sort_values('weight', ascending=False)[['name']] FINAL RESULT After filtering weight > 100: name Elephant 150 kg > 100 kg Output Array: ["Elephant"] OK - Done! Only Elephant meets the weight criteria Lion rejected (80 < 100) Key Insight: Method chaining allows you to perform multiple DataFrame operations in a single, readable expression. Each method returns a DataFrame, enabling the next method call. This pattern: filter --> sort --> select is very common. TutorialsPoint - Method Chaining | Pandas DataFrame Operations
Asked in
Meta 25 Google 20 Amazon 15
31.5K Views
Medium Frequency
~8 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen