Would like to pull a single value of a python dictionary nested in a pandas dataframe

Viewed 28

I'm working with a dictionary nested in a dataframe and would like to pull a single value out of one of a dictionary.

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

Been trying something along these lines but isn't the correct syntax:

fullFrameData.iloc[1[0]]['flow1']

fullFrameData.iloc[0][0]['flow2']

fullFrameData.iloc['outside][0]['flow1']

Thanks!

1 Answers

Good try on the attempts however I've run into this before and you can solve it by this -

print ("Test2: {0}".format(fullFrameData.iloc[1]['flow1']['outside']))

This returns 10.0.0.12 as you're looking for. The reason this is the case is because you need to grab the key from within 'flow1' which isn't going to be indexed by an integer.

The logic is that you're grabbing the second dict [1] within 'flow1' which then you need to give it the key of 'outside'.

Related