Completed trips by city

aggregations

Completed 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 NameType
trip_idint64
rider_idint64
driver_idint64
citystr
requested_atstr
statusstr
distance_kmfloat64
farefloat64

tripsExample Input

trip_idrider_iddriver_idcityrequested_atstatusdistance_kmfare
9001301401Chicago2024-05-03 07:42:00completed8.418.9
9002302402Chicago2024-05-03 08:15:00completed3.19.5
9003303403Austin2024-05-03 08:47:00rider_canceled00
9004304404Austin2024-05-03 12:05:00completed12.624.3
9005305401Chicago2024-05-03 17:30:00completed5.213.4
9006306405Seattle2024-05-03 17:55:00driver_canceled00
9007307406Seattle2024-05-03 18:10:00completed9.826.1
9008308402Chicago2024-05-03 18:25:00completed4.411.2

Example Output

citycompleted_tripstotal_kmavg_fare
Austin112.624.3
Chicago421.113.25
Seattle19.826.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.