0

I have a dataframe where some columns are formatted this way:

example value of ColX:

 {variable1={key:value,key2:value2.....}, variable2={key3:value3,key4:value4.....}}

Let's say I want to access value2 and value4. How can I address them using Python?

My desired result is to make a column key2 with value2's and column key4 with all the value4's

Sorry. I could not google the name of this data structure. It's something like a list of dictionary definitions packed into strings.

Krzysztof Dołęgowski
  • 2,583
  • 1
  • 5
  • 21

2 Answers2

0

You are using dictionnaries of dictionnaries (I think). In order to get to value 2 you need to get first to variable 1, and then to key2 so you can do this, if your dictionnary is stored in ColX :

ColX.get(variable1).get(key2)

By the way are you sure of the syntax of ColX ? Isn't it :

{variable1:{key:value,key2:value2.....}, variable2:{key3:value3,key4:value4.....}}
  • Yes. I am sure about this syntax. This "=" makes all the confusion. But you just brought the idea that I can replace all the "=" with ":" and have nested dictionaries. And this might solve the puzzle. – Krzysztof Dołęgowski Oct 17 '22 at 11:56
0

The data structure is simply a nested dict. Try some of the answers given here: Safe method to get value of nested dictionary. Dict methods can be concatenated, so you can do:

dictionary.get(keyname, value).get(keyname, value)

m-eriksen
  • 61
  • 1
  • 4
  • Are you sure? I thought nested dictionary looks like this: { 'dictA': {'key_1': 'value_1'}, 'dictB': {'key_2': 'value_2'}} . but here I clearly see "=" sign – Krzysztof Dołęgowski Oct 17 '22 at 11:58
  • There must be an underlying datastructure as RJ Adriaansen points out. That object is merely a wrapper, so you should investigate what the object uses to build that "dict-like" structure. – m-eriksen Oct 17 '22 at 12:52