Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0bdcf5d

Browse files
Add file
1 parent f1eae45 commit 0bdcf5d

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
2891. Method Chaining
3+
Solved
4+
Easy
5+
Companies
6+
DataFrame animals
7+
+-------------+--------+
8+
| Column Name | Type |
9+
+-------------+--------+
10+
| name | object |
11+
| species | object |
12+
| age | int |
13+
| weight | int |
14+
+-------------+--------+
15+
Write a solution to list the names of animals that weigh strictly more than 100 kilograms.
16+
17+
Return the animals sorted by weight in descending order.
18+
19+
The result format is in the following example.
20+
21+
Example 1:
22+
23+
Input:
24+
DataFrame animals:
25+
+----------+---------+-----+--------+
26+
| name | species | age | weight |
27+
+----------+---------+-----+--------+
28+
| Tatiana | Snake | 98 | 464 |
29+
| Khaled | Giraffe | 50 | 41 |
30+
| Alex | Leopard | 6 | 328 |
31+
| Jonathan | Monkey | 45 | 463 |
32+
| Stefan | Bear | 100 | 50 |
33+
| Tommy | Panda | 26 | 349 |
34+
+----------+---------+-----+--------+
35+
Output:
36+
+----------+
37+
| name |
38+
+----------+
39+
| Tatiana |
40+
| Jonathan |
41+
| Tommy |
42+
| Alex |
43+
+----------+
44+
Explanation:
45+
All animals weighing more than 100 should be included in the results table.
46+
Tatiana's weight is 464, Jonathan's weight is 463, Tommy's weight is 349, and Alex's weight is 328.
47+
The results should be sorted in descending order of weight.
48+
49+
In Pandas, method chaining enables us to perform operations on a DataFrame without breaking up each operation into a separate line or creating multiple temporary variables.
50+
51+
Can you complete this task in just one line of code using method chaining?"
52+
"""
53+
54+
import pandas as pd
55+
56+
def findHeavyAnimals(animals: pd.DataFrame) -> pd.DataFrame:
57+
filtered_animals = animals[animals['weight'] > 100].sort_values('weight', ascending = False)
58+
return filtered_animals[['name']]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /