我已经看到很多关于如何按层次排列数据帧行索引的示例,但我正在尝试对列执行相同的操作并且不理解语法:
鉴于:
df = pd.DataFrame(np.random.randn(10,10),
columns=['consumption', 'voltage', 'consumption',
'voltage', 'temperature', 'humidity', 'consumption',
'voltage','temperature','humidity'],
index= pd.date_range('20000103',periods=10))
>>> df
consumption voltage consumption voltage temperature \
2000-01-03 -1.327735 -1.440285 0.317122 -1.120105 1.736651
2000-01-04 0.132531 0.646972 2.296734 0.332154 -0.541792
2000-01-05 0.127623 0.592778 0.162096 0.107398 -0.628785
2000-01-06 -1.441151 0.215424 0.021068 0.683085 -0.783994
2000-01-07 -0.157848 1.566780 0.599017 -0.628216 0.500251
2000-01-08 -0.498926 0.338771 0.400159 1.571975 0.255635
2000-01-09 0.516618 -1.936360 0.199388 -0.110415 2.690859
2000-01-10 -0.779012 -1.310022 -1.207503 0.095679 -0.134244
2000-01-11 0.644262 0.068196 1.041745 -0.444408 -0.751595
2000-01-12 -0.608046 0.506588 …
Run Code Online (Sandbox Code Playgroud) 我正在使用MS Scripting Runtime 库中的字典对象来存储一系列数组并根据需要对数组单元执行操作。有一个 for 循环来完成创建所有这些条目的过程。我的问题是,在使用该.exists
属性时,它True
甚至在添加项目之前就返回了。
更仔细的调试表明在 for 循环开始时将键添加到字典中,即使没有.add
使用任何命令并且直到循环结束才会使用。
我尝试了几种不同的配置,但这是一个失败的简单示例:
Dim dTotals As Dictionary
Set dTotals = New Dictionary
dTotals.CompareMode = BinaryCompare
For Each cell In rAppID
If Not dTotals.Exists(cell) Then
Set rAppIDCells = Find_Range(cell, rAppID)
Set rAppIDValues = rAppIDCells.Offset(0, 6)
dAppIDTotal = WorksheetFunction.Sum(rAppIDValues)
dTotals.Add Key:=cell.Value, Item:=dAppIDTotal
End If
Next cell
Run Code Online (Sandbox Code Playgroud)
每个单元格包含一个字符串/唯一 ID。在 If 语句中,代码返回 false,即使在第一次迭代时也是如此。
使用pandas/pytables,可以轻松返回键列表store.keys()
.
>>> store.keys()
['/df_coord', '/metaFrame']
Run Code Online (Sandbox Code Playgroud)
使用标准字典检查以查看密钥是否存在,if 'df_coord' in store.keys():
除非/
包含密钥,否则返回false .是否有另一种简单的方法来评估密钥的存在而无需连接字符串?