AnalystPath

EV Charging Spend per Vehicle

PandasMediumMid level~10 min

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.

charging_sessions
station_idvehicle_idplug_inplug_outamount_paid
190012024-07-01 08:00:002024-07-01 10:30:005.0
190012024-07-02 11:00:002024-07-02 12:45:003.0
290012024-07-01 10:45:002024-07-01 12:00:006.0
290022024-07-01 09:00:002024-07-01 11:30:004.0
390012024-07-03 07:00:002024-07-03 09:00:004.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_sessions

Solve 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

Related Pandas questions