Quantcast
Channel: How to use a dot "." to access members of dictionary? - Stack Overflow
Viewing all articles
Browse latest Browse all 40

Answer by Daniel for How to use a dot "." to access members of dictionary?

$
0
0

If you're already using pandas, you can construct a pandas Series or DataFrame from which you would be able to access items via the dot syntax:

1-level dictionary:

import pandas as pdmy_dictionary = pd.Series({'key1': 'value1','key2': 'value2'})print(my_dictionary.key1)# Output: value1

2-level dictionary:

import pandas as pdmy_dictionary = pd.DataFrame({'key1': {'inner_key1': 'value1'  },'key2': {'inner_key2': 'value2'  }})print(my_dictionary.key1.inner_key1)# Output: value1

Be aware that this probably works better with a normalised data structure (where each dictionary entry has the same structure). In the second example above, the resulting DataFrame is:

              key1    key2inner_key1  value1     NaNinner_key2     NaN  value2

Viewing all articles
Browse latest Browse all 40

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>