【发布时间】:2019-08-23 01:48:58
【问题描述】:
我无法创建显示年度能源使用数据的新数据表。基本上,我想将能源乘以不同的因素来显示每年的能源使用量。
代码如下。
#calculate energy amounts
energy_use_by_fuel = pd.DataFrame()
for hhid in energy_data.hhid.unique():
tempdtf = pd.DataFrame({
'hhid':hhid,
'monthly_electricity': energy_data.loc[energy_data.hhid == hhid, 'estimated_kwh_monthly']*3,
'monthly_gas': energy_data.loc[energy_data.hhid == hhid, 'monthly_gas_use_kg'] * 4,
'monthly_charcoal': energy_data.loc[energy_data.hhid == hhid,
'monthly_charcoal_use_kg'] * 5})
#join
tempdtf = energy_use_by_fuel.append(tempdtf, ignore_index = True)
如您所见,我想计算电力、天然气和木炭的不同能源用途。但是当我将数据乘以数字时,生成的数据框energy_use_by_fuel 为空。
【问题讨论】:
-
欢迎来到 Stack Overflow!您能否编辑来自
energy_data的问题特定数据以及预期的输出?这将帮助其他人帮助解决问题。
标签: python pandas dataframe for-loop