EV Charging Spend per Vehicle
Problem
A network of charging stations records each visit in a DataFrame `charging_sessions` with columns `station_id`, `vehicle_id`, `plug_in`, `plug_out` (datetime strings), and `amount_paid`.
For every vehicle, return:
- `total_paid`: the sum of all amounts it paid.
- `avg_hourly_cost`: total amount paid divided by total hours plugged in, rounded to 2 decimals.
- `top_station`: the `station_id` where the vehicle spent the most total time; if two stations are tied, choose the smaller `station_id`.
Return columns `vehicle_id`, `total_paid`, `avg_hourly_cost`, `top_station`, ordered by `vehicle_id`.
Input data
Example rows — the live problem includes the full dataset.
| station_id | vehicle_id | plug_in | plug_out | amount_paid |
|---|---|---|---|---|
| 1 | 9001 | 2024-07-01 08:00:00 | 2024-07-01 10:30:00 | 5.0 |
| 1 | 9001 | 2024-07-02 11:00:00 | 2024-07-02 12:45:00 | 3.0 |
| 2 | 9001 | 2024-07-01 10:45:00 | 2024-07-01 12:00:00 | 6.0 |
| 2 | 9002 | 2024-07-01 09:00:00 | 2024-07-01 11:30:00 | 4.0 |
| 3 | 9001 | 2024-07-03 07:00:00 | 2024-07-03 09:00:00 | 4.0 |
Expected output
Your answer should return 2 rows with the columns vehicle_id, total_paid, avg_hourly_cost, top_station.
Starter code (Pandas (Python))
import pandas as pd
def ev_charging_spend_per_vehicle(charging_sessions) -> pd.DataFrame:
# Your code here
return charging_sessionsSolve this Pandas question free
Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock