𝗘𝗹𝗲𝗰𝘁𝗿𝗶𝗰𝗶𝘁𝘆 𝗖𝗼𝗻𝘀𝘂𝗺𝗽𝘁𝗶𝗼𝗻
🔶You have access to data from an electricity billing system, detailing the electricity usage and cost for specific households over billing periods in the years 2023 and 2024. Your objective is to present the total electricity consumption, total cost and average monthly consumption for each household per year display the output in ascending order of each household id & year of the bill.
📌𝗣𝗮𝗻𝗱𝗮𝘀 𝗰𝗼𝗱𝗲:
🔶𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵-𝟭-
import pandas as pd
import datetime as dt
#Converting to datetime
electricity_bill_df["billing_period"]=pd.to_datetime(electricity_bill_df["billing_period"])
#extracting the year
electricity_bill_df["bill_year"]=electricity_bill_df["billing_period"].dt.year
#Aggregrating the data
electricity_bill_df_ans=electricity_bill_df.groupby(["household_id", "bill_year"]).agg(
total_cost=("total_cost","sum"),
consumption_kwh=("consumption_kwh","sum"),
avg_consumption_kwh=("consumption_kwh","mean")
).reset_index()
#Sorting the data
electricity_bill_df_ans=electricity_bill_df_ans.sort_values(by=['household_id', 'bill_year'])
print(electricity_bill_df_ans)
No comments:
Post a Comment