1. 求和
b = {0: 125, 1: 129, 2: 207, 3: 278, 4: 199, 5: 126, 6: 60, 7: 190, 8: 19, 9: 67, 10: 260, 11: 73, 12: 128}
b_tmp = dict((key, value) for key, value in b.items() if key in [1,9,12,7])
print(sum(b_tmp.values()))
2. 字典过滤条件
d = {
'a': '0.0000',
'b': '1.2'
}
d_tmp = dict((key, value) for key, value in d.items() if float(value) > 0)
print(d_tmp)
output
{'b': '1.2'}
3. 更改key
temp_out[tower_id] = temp_out.pop('ws_Avg')
这行代码的作用是将字典或DataFrame temp_out 中的键(列)名 ws_Avg 重命名为 tower_id,并保留原来的数据值。具体地说:
temp_out.pop('ws_Avg'):使用pop方法从temp_out中移除'ws_Avg'键,并返回其对应的值。pop方法会删除ws_Avg键,因此在temp_out中不再存在这个键。temp_out[tower_id] = ...:将ws_Avg键的值赋给一个新的键tower_id。
解释示例
假设 temp_out 的原始结构如下:
temp_out = {
'ws_Avg': [3.4, 5.6, 7.1],
'ws_SD': [0.5, 0.6, 0.7]
}
tower_id = '0001'
执行 temp_out[tower_id] = temp_out.pop('ws_Avg') 后,temp_out 将变成:
temp_out = {
'0001': [3.4, 5.6, 7.1], # 原来的 'ws_Avg' 值现在存储在 '0001' 键下
'ws_SD': [0.5, 0.6, 0.7]
}
总结
该行代码将 temp_out 中的 'ws_Avg' 列名或键名替换为 tower_id,并保留原始的列或键的数据值。
159



被折叠的 条评论
为什么被折叠?



