Solar Panels Generating More Per Sun-Hour
Problem
You are given two DataFrames. `panels` has columns `panel_id` and `site_label`. `generation_logs` has columns `log_id`, `panel_id`, `logged_on`, `energy_kwh`, and `sun_hours`; the yield for a log is `energy_kwh` divided by `sun_hours`.
Find every panel whose average yield improved from the first half of the year (January through June) to the second half (July through December). Only include panels that have at least one log in each half. Report the gain as the second-half average yield minus the first-half average yield, rounded to two decimals.
Return a DataFrame with columns `site_label` and `yield_gain`, ordered by `yield_gain` descending, then `site_label` ascending.
Input data
Example rows — the live problem includes the full dataset.
| panel_id | site_label |
|---|---|
| 1 | Rooftop A |
| 2 | Rooftop B |
| 3 | Carport C |
| 4 | Field D |
| 5 | Field E |
| log_id | panel_id | logged_on | energy_kwh | sun_hours |
|---|---|---|---|---|
| 1 | 1 | 2024-02-10 | 40 | 8 |
| 2 | 1 | 2024-03-10 | 45 | 9 |
| 3 | 1 | 2024-08-10 | 70 | 7 |
| 4 | 1 | 2024-09-10 | 60 | 6 |
| 5 | 2 | 2024-01-15 | 30 | 6 |
Expected output
Your answer should return 3 rows with the columns site_label, yield_gain.
Starter code (Pandas (Python))
import pandas as pd
def solar_panels_generating_more_per_sun_hour(panels, generation_logs) -> pd.DataFrame:
# Your code here
return panelsSolve 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