df_devices = df.groupby(['from_time', 'device', 'type'])['power'].mean()
在计算列中mean()
的{{1}}之后,我得到了一个包含以下内容的熊猫系列
groupby
我想将其转换为以下格式的字典:
from_time device type
00:00:00 AC indoor 1362.214286
outdoor 3470.705882
Computer indoor 399.000000
outdoor 412.400000
Heater indoor 2258.375000
outdoor 2274.666667
Lights indoor 1535.000000
outdoor 3475.473684
Microwave indoor 1420.000000
outdoor 1489.933333
Refridgerator indoor 192.388889
outdoor 195.076923
Television indoor 243.666667
outdoor 261.500000
01:00:00 AC indoor 1493.071429
outdoor 3724.352941
Computer indoor 416.461538
outdoor 413.555556
Heater indoor 2143.277778
outdoor 2286.461538
Lights indoor 1447.950000
outdoor 3092.454545
Microwave indoor 1536.857143
outdoor 1429.294118
Refridgerator indoor 207.416667
outdoor 234.684211
Television indoor 251.000000
outdoor 249.904762
我确实在框架上尝试过{'00:00:00' : {'AC': {'indoor': 1362.2142857142858, 'outdoor': 3470.705882352941},
'Computer': {'indoor': 399.0, 'outdoor': 412.4}, 'Heater': {'indoor':
2258.375, 'outdoor': 2274.6666666666665}, 'Lights': {'indoor': 1535.0,
'outdoor': 3475.4736842105262}, 'Microwave': {'indoor': 1420.0, 'outdoor':
1489.9333333333334}, 'Refridgerator': {'indoor': 192.38888888888889,
'outdoor': 195.07692307692307}, 'Television': {'indoor': 243.66666666666666,
'outdoor': 261.5}},
'00:00:01' : {'AC': {'indoor': 1493.0714285714287, 'outdoor': 3724.3529411764707},
'Computer': {'indoor': 416.46153846153845, 'outdoor': 413.55555555555554},
'Heater': {'indoor': 2143.277777777778, 'outdoor': 2286.4615384615386},
'Lights': {'indoor': 1447.95, 'outdoor': 3092.4545454545455}, 'Microwave':
{'indoor': 1536.857142857143, 'outdoor': 1429.2941176470588},
'Refridgerator': {'indoor': 207.41666666666666, 'outdoor':
234.68421052631578}, 'Television': {'indoor': 251.0, 'outdoor':
249.9047619047619}}}
和.agg()
,但最终使用了不同的格式
.unstack()
也没有给出令人满意的结果。它返回不同的格式
使用以下代码,我能做到的最好
.to_dict()
不给出预期的输出
df_devices.unstack(level=2).unstack().to_dict('index')
答案 0 :(得分:0)
我假设数据帧是逗号分隔的数据帧,并以名称a.csv存储。 以下是一种方法,您可以遍历每一行并从数据框中形成字典。
<div id="PRODUCT_DETAILS" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel"><?php echo $row['pro_name'];?></h4>
</div>
<div class="modal-body product_detail">
<div class="col-md-5 modal_body_left">
<img src="../products_images/<?php echo $row['pro_img']; ?>" alt=" " class="width-azset img-responsive">
</div>
<div class="col-md-7 modal_body_right">
<h4><?php echo $row['pro_name'];?></h4>
<p><?php echo $row['pro_dis'];?></p>
<div class="modal_body_right_cart simpleCart_shelfItem">
<p><i class="item_price"><?php echo $row['pro_price'];?></i></p>
</div>
</div>
<div class="clearfix"> </div>
</div>
<div class="modal-footer">
<button type="submit" name="Add_Cart" class="btn btn-success">Add to Cart</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
如果您找到比这更好的东西,请告诉我。 谢谢。希望对您有所帮助。
答案 1 :(得分:0)
我能够使用与我创建的相同的dict
来解决
dicto = df_devices.unstack(level=2).unstack().to_dict('index')
>>> dicto
{'00:00:00': {('indoor', 'AC'): 1362.2142857142858,
('indoor', 'Computer'): 399.0,
('indoor', 'Heater'): 2258.375,
('indoor', 'Lights'): 1535.0,
('indoor', 'Microwave'): 1420.0,
('indoor', 'Refridgerator'): 192.38888888888889,
('indoor', 'Television'): 243.66666666666666,
('outdoor', 'AC'): 3470.705882352941,
('outdoor', 'Computer'): 412.4,
('outdoor', 'Heater'): 2274.6666666666665,
('outdoor', 'Lights'): 3475.4736842105262,
('outdoor', 'Microwave'): 1489.9333333333334,
('outdoor', 'Refridgerator'): 195.07692307692307,
('outdoor', 'Television'): 261.5}
浏览创建的字典,
df_dict = {}
df_type = {}
for i, j in dicto.items():
for key, value in j.items():
df_type[key[1]] = value
df_dict[key[0]] = df_type.copy()
dicto[i] = df_dict.copy()
.copy()
用于创建副本而不是引用
>>> dicto
{'00:00:00': {'AC': {'indoor': 1362.2142857142858,
'outdoor': 3470.705882352941},
'Computer': {'indoor': 399.0, 'outdoor': 412.4},
'Heater': {'indoor': 2258.375, 'outdoor': 2274.6666666666665},
'Lights': {'indoor': 1535.0, 'outdoor': 3475.4736842105262},
'Microwave': {'indoor': 1420.0, 'outdoor': 1489.9333333333334},
'Refridgerator': {'indoor': 192.38888888888889,
'outdoor': 195.07692307692307},
'Television': {'indoor': 243.66666666666666, 'outdoor': 261.5}}.........