Convert string to dict, then access key:values??? How to access data in a <class 'dict'> for Python?
<p>I am having issues accessing data inside a dictionary.  </p>

<blockquote>
  <p>Sys: Macbook 2012 <br>
  Python: Python 3.5.1 :: Continuum Analytics, Inc.</p>
</blockquote>

<p>I am working with a <a href="http://dask.pydata.org/en/latest/dataframe-create.html" rel="nofollow">dask.dataframe</a> created from a csv.  </p>

<h2>Edit Question</h2>

<h1>How I got to this point</h1>

<p>Assume I start out with a Pandas Series:</p>

<pre><code>df.Coordinates
130      {u'type': u'Point', u'coordinates': [-43.30175...
278      {u'type': u'Point', u'coordinates': [-51.17913...
425      {u'type': u'Point', u'coordinates': [-43.17986...
440      {u'type': u'Point', u'coordinates': [-51.16376...
877      {u'type': u'Point', u'coordinates': [-43.17986...
1313     {u'type': u'Point', u'coordinates': [-49.72688...
1734     {u'type': u'Point', u'coordinates': [-43.57405...
1817     {u'type': u'Point', u'coordinates': [-43.77649...
1835     {u'type': u'Point', u'coordinates': [-43.17132...
2739     {u'type': u'Point', u'coordinates': [-43.19583...
2915     {u'type': u'Point', u'coordinates': [-43.17986...
3035     {u'type': u'Point', u'coordinates': [-51.01583...
3097     {u'type': u'Point', u'coordinates': [-43.17891...
3974     {u'type': u'Point', u'coordinates': [-8.633880...
3983     {u'type': u'Point', u'coordinates': [-46.64960...
4424     {u'type': u'Point', u'coordinates': [-43.17986...
</code></pre>

<p>The problem is, this is not a true dataframe of dictionaries.  Instead, it's a column full of strings that LOOK like dictionaries.  Running this show it:</p>

<pre><code>df.Coordinates.apply(type)
130      &lt;class 'str'&gt;
278      &lt;class 'str'&gt;
425      &lt;class 'str'&gt;
440      &lt;class 'str'&gt;
877      &lt;class 'str'&gt;
1313     &lt;class 'str'&gt;
1734     &lt;class 'str'&gt;
1817     &lt;class 'str'&gt;
1835     &lt;class 'str'&gt;
2739     &lt;class 'str'&gt;
2915     &lt;class 'str'&gt;
3035     &lt;class 'str'&gt;
3097     &lt;class 'str'&gt;
3974     &lt;class 'str'&gt;
3983     &lt;class 'str'&gt;
4424     &lt;class 'str'&gt;
</code></pre>

<p><strong>My Goal</strong>: Access the <code>coordinates</code> key and value in the dictionary.  That's it. But it's a <code>str</code> </p>

<p>I converted the strings to dictionaries using <code>eval</code>.</p>

<pre><code>new = df.Coordinates.apply(eval)
130      {'coordinates': [-43.301755, -22.990065], 'typ...
278      {'coordinates': [-51.17913026, -30.01201896], ...
425      {'coordinates': [-43.17986794, -22.91000096], ...
440      {'coordinates': [-51.16376782, -29.95488677], ...
877      {'coordinates': [-43.17986794, -22.91000096], ...
1313     {'coordinates': [-49.72688407, -29.33757253], ...
1734     {'coordinates': [-43.574057, -22.928059], 'typ...
1817     {'coordinates': [-43.77649254, -22.86940539], ...
1835     {'coordinates': [-43.17132318, -22.90895217], ...
2739     {'coordinates': [-43.1958313, -22.98755333], '...
2915     {'coordinates': [-43.17986794, -22.91000096], ...
3035     {'coordinates': [-51.01583481, -29.63593292], ...
3097     {'coordinates': [-43.17891379, -22.96476163], ...
3974     {'coordinates': [-8.63388008, 41.14594453], 't...
3983     {'coordinates': [-46.64960938, -23.55902666], ...
4424     {'coordinates': [-43.17986794, -22.91000096], ...
</code></pre>

<p>Next I text the type of object and get:</p>

<pre><code>130      &lt;class 'dict'&gt;
278      &lt;class 'dict'&gt;
425      &lt;class 'dict'&gt;
440      &lt;class 'dict'&gt;
877      &lt;class 'dict'&gt;
1313     &lt;class 'dict'&gt;
1734     &lt;class 'dict'&gt;
1817     &lt;class 'dict'&gt;
1835     &lt;class 'dict'&gt;
2739     &lt;class 'dict'&gt;
2915     &lt;class 'dict'&gt;
3035     &lt;class 'dict'&gt;
3097     &lt;class 'dict'&gt;
3974     &lt;class 'dict'&gt;
3983     &lt;class 'dict'&gt;
4424     &lt;class 'dict'&gt;
</code></pre>

<p>If I try to access my dictionaries:
new.apply(lambda x: x['coordinates']</p>

<pre><code>---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-71-c0ad459ed1cc&gt; in &lt;module&gt;()
----&gt; 1 dfCombined.Coordinates.apply(coord_getter)

/Users/linwood/anaconda/envs/dataAnalysisWithPython/lib/python3.5/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
   2218         else:
   2219             values = self.asobject
-&gt; 2220             mapped = lib.map_infer(values, f, convert=convert_dtype)
   2221 
   2222         if len(mapped) and isinstance(mapped[0], Series):

pandas/src/inference.pyx in pandas.lib.map_infer (pandas/lib.c:62658)()

&lt;ipython-input-68-748ce2d8529e&gt; in coord_getter(row)
      1 import ast
      2 def coord_getter(row):
----&gt; 3     return (ast.literal_eval(row))['coordinates']

TypeError: 'bool' object is not subscriptable
</code></pre>

<p>It's some type of class, because when I run <code>dir</code> I get this for one object:</p>

<pre><code>new.apply(lambda x: dir(x))[130]
130           __class__
130        __contains__
130         __delattr__
130         __delitem__
130             __dir__
130             __doc__
130              __eq__
130          __format__
130              __ge__
130    __getattribute__
130         __getitem__
130              __gt__
130            __hash__
130            __init__
130            __iter__
130              __le__
130             __len__
130              __lt__
130              __ne__
130             __new__
130          __reduce__
130       __reduce_ex__
130            __repr__
130         __setattr__
130         __setitem__
130          __sizeof__
130             __str__
130    __subclasshook__
130               clear
130                copy
130            fromkeys
130                 get
130               items
130                keys
130                 pop
130             popitem
130          setdefault
130              update
130              values
Name: Coordinates, dtype: object
</code></pre>

<p><strong>My Problem</strong>: I just want to access the dictionary.  But, the object is <code>&lt;class 'dict'&gt;</code>.  How do I covert this to a regular dict or just access the key:value pairs?</p>

<p>Any ideas??</p>

<p>It looks like you end up with something like this</p>

<pre><code>s = pd.Series([
        dict(type='Point', coordinates=[1, 1]),
        dict(type='Point', coordinates=[1, 2]),
        dict(type='Point', coordinates=[1, 3]),
        dict(type='Point', coordinates=[1, 4]),
        dict(type='Point', coordinates=[1, 5]),
        dict(type='Point', coordinates=[2, 1]),
        dict(type='Point', coordinates=[2, 2]),
        dict(type='Point', coordinates=[2, 3]),        
    ])

s

0    {u'type': u'Point', u'coordinates': [1, 1]}
1    {u'type': u'Point', u'coordinates': [1, 2]}
2    {u'type': u'Point', u'coordinates': [1, 3]}
3    {u'type': u'Point', u'coordinates': [1, 4]}
4    {u'type': u'Point', u'coordinates': [1, 5]}
5    {u'type': u'Point', u'coordinates': [2, 1]}
6    {u'type': u'Point', u'coordinates': [2, 2]}
7    {u'type': u'Point', u'coordinates': [2, 3]}
dtype: object
</code></pre>

<h3>Solution</h3>

<pre><code>df = s.apply(pd.Series)
df
</code></pre>

<p><a href="http://i.stack.imgur.com/loUCd.png" rel="nofollow"><img src="http://i.stack.imgur.com/loUCd.png" alt="enter image description here"></a></p>

<p>then access coordinates</p>

<pre><code>df.coordinates

0    [1, 1]
1    [1, 2]
2    [1, 3]
3    [1, 4]
4    [1, 5]
5    [2, 1]
6    [2, 2]
7    [2, 3]
Name: coordinates, dtype: object
</code></pre>

<p>Or even</p>

<pre><code>df.coordinates.apply(pd.Series)
</code></pre>

<p><a href="http://i.stack.imgur.com/JMyIe.png" rel="nofollow"><img src="http://i.stack.imgur.com/JMyIe.png" alt="enter image description here"></a></p>

python pandas dictionary data-manipulation dask