
Completed trips by city
easyCompleted trips by city
Uber Pandas Interview Question
Uber's city dashboard summarizes completed trips each day.
Use the trips DataFrame and count completed trips only. Return each city with the number of trips as completed_trips, the total distance as total_km, rounded to 1 decimal place, and the average fare as avg_fare, rounded to 2 decimal places. Sort the rows by city. Assign the answer to result.
Asked of
- Data Analyst
- Product Analyst
- Business Analyst
- Analytics Engineer
- Data Scientist
tripsDataFrame14 rows
| Column Name | Type |
|---|---|
| trip_id | int64 |
| rider_id | int64 |
| driver_id | int64 |
| city | str |
| requested_at | str |
| status | str |
| distance_km | float64 |
| fare | float64 |
tripsExample Input
| trip_id | rider_id | driver_id | city | requested_at | status | distance_km | fare |
|---|---|---|---|---|---|---|---|
| 9001 | 301 | 401 | Chicago | 2024-05-03 07:42:00 | completed | 8.4 | 18.9 |
| 9002 | 302 | 402 | Chicago | 2024-05-03 08:15:00 | completed | 3.1 | 9.5 |
| 9003 | 303 | 403 | Austin | 2024-05-03 08:47:00 | rider_canceled | 0 | 0 |
| 9004 | 304 | 404 | Austin | 2024-05-03 12:05:00 | completed | 12.6 | 24.3 |
| 9005 | 305 | 401 | Chicago | 2024-05-03 17:30:00 | completed | 5.2 | 13.4 |
| 9006 | 306 | 405 | Seattle | 2024-05-03 17:55:00 | driver_canceled | 0 | 0 |
| 9007 | 307 | 406 | Seattle | 2024-05-03 18:10:00 | completed | 9.8 | 26.1 |
| 9008 | 308 | 402 | Chicago | 2024-05-03 18:25:00 | completed | 4.4 | 11.2 |
Example Output
| city | completed_trips | total_km | avg_fare |
|---|---|---|---|
| Austin | 1 | 12.6 | 24.3 |
| Chicago | 4 | 21.1 | 13.25 |
| Seattle | 1 | 9.8 | 26.1 |
Explanation
In the example, Chicago's 4 completed trips covered 8.4, 3.1, 5.2 and 4.4 km, 21.1 km in total, with an average fare of 13.25. The first Austin trip was canceled by the rider, so only one Austin trip counts.
The example above is a small slice of the data. Your code runs against the full DataFrames.
Company
Uber
Difficulty
easy
Topic
aggregations
Language
Pandas
Your code
Loading Python in the background. You can start writing.