我正在使用嵌套在数据框中的字典,并想从其中一个字典中提取单个值。
asdf = {'outside':'10.0.0.14', 'inside':'10.7.0.12', 'name': 'qqqq'}
sdfg = {'outside':'10.0.0.12', 'inside':'10.7.0.14', 'name' : 'zzzz'}
bbbb = ['123.123.123.123', '22.22.22.22', 'foo'] ##also trying it with a list vs dictionary
fullFrameData= pd.DataFrame({
'flow1' : [asdf, sdfg, 240, 1200, 250, 1472],
'flow2' : [bbbb, asdf, 240, 1005, 250, 1472],
})
print ("Test1: {0}".format(fullFrameData.iloc[2]['flow1']))
##returns: 240
print ("Test2: {0}".format(fullFrameData.iloc[1]['flow1']))
##returns: {'outside':'10.0.0.12', 'inside':'10.7.0.14', 'name' : 'zzzz'}
print ("Test3: {0}".format(fullFrameData.iloc[1]['flow2']))
##returns: ['123.123.123.123', '22.22.22.22', 'foo']
I'd like to figure out how to return the 'outside' value or any of the array values by referencing fullFrameData
一直在尝试这些方法,但语法不正确:
fullFrameData.iloc[1[0]]['flow1']
fullFrameData.iloc[0][0]['flow2']
fullFrameData.iloc['outside][0]['flow1']
谢谢!
最佳答案
很好的尝试,但是我以前遇到过这个,你可以通过这个解决它 -
print ("Test2: {0}".format(fullFrameData.iloc[1]['flow1']['outside']))
这将返回您正在查找的 10.0.0.12。出现这种情况的原因是因为您需要从不会被整数索引的“flow1”中获取 key 。
逻辑是你在 'flow1' 中获取第二个字典 [1] 然后你需要给它 'outside' 的键。
关于python - 想提取嵌套在 Pandas 数据框中的 python 字典的单个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68748281/