title: How to json_normalize a column with NaNs
body: <ul>
  <li>This question is specific to columns of data in a <code>pandas.DataFrame</code></li>
  <li>This question depends on if the values in the columns are <code>str</code>, <code>dict</code>, or <code>list</code> type.</li>
  <li>This question addresses dealing with the <code>NaN</code> values, when <code>df.dropna().reset_index(drop=True)</code> isn't a valid option.</li>
  </ul>
  <h2>Case 1</h2>
  <ul>
  <li>With a column of <code>str</code> type, the values in the column must be converted to <code>dict</code> type, with <code>ast.literal_eval</code>, before using <code>.json_normalize</code>.</li>
  </ul>
  <pre class="lang-py prettyprint-override"><code>import numpy as np
  import pandas as pd
  from ast import literal_eval
  
  df = pd.DataFrame({'col_str': ['{&quot;a&quot;: &quot;46&quot;, &quot;b&quot;: &quot;3&quot;, &quot;c&quot;: &quot;12&quot;}', '{&quot;b&quot;: &quot;2&quot;, &quot;c&quot;: &quot;7&quot;}', '{&quot;c&quot;: &quot;11&quot;}', np.NaN]})
  
                              col_str
  0  {&quot;a&quot;: &quot;46&quot;, &quot;b&quot;: &quot;3&quot;, &quot;c&quot;: &quot;12&quot;}
  1              {&quot;b&quot;: &quot;2&quot;, &quot;c&quot;: &quot;7&quot;}
  2                       {&quot;c&quot;: &quot;11&quot;}
  3                               NaN
  
  type(df.iloc[0, 0])
  [out]: str
  
  df.col_str.apply(literal_eval)
  </code></pre>
  <p>Error:</p>
  <pre class="lang-py prettyprint-override"><code>df.col_str.apply(literal_eval) results in ValueError: malformed node or string: nan
  </code></pre>
  <h2>Case 2</h2>
  <ul>
  <li>With a column of <code>dict</code> type, use <code>pandas.json_normalize</code> to convert keys to column headers and values to rows</li>
  </ul>
  <pre class="lang-py prettyprint-override"><code>df = pd.DataFrame({'col_dict': [{&quot;a&quot;: &quot;46&quot;, &quot;b&quot;: &quot;3&quot;, &quot;c&quot;: &quot;12&quot;}, {&quot;b&quot;: &quot;2&quot;, &quot;c&quot;: &quot;7&quot;}, {&quot;c&quot;: &quot;11&quot;}, np.NaN]})
  
                             col_dict
  0  {'a': '46', 'b': '3', 'c': '12'}
  1              {'b': '2', 'c': '7'}
  2                       {'c': '11'}
  3                               NaN
  
  type(df.iloc[0, 0])
  [out]: dict
  
  pd.json_normalize(df.col_dict)
  </code></pre>
  <p>Error:</p>
  <pre class="lang-py prettyprint-override"><code>pd.json_normalize(df.col_dict) results in AttributeError: 'float' object has no attribute 'items'
  </code></pre>
  <h2>Case 3</h2>
  <ul>
  <li>In a column of <code>str</code> type, with the <code>dict</code> inside a <code>list</code>.</li>
  <li>To normalize the column
  <ul>
  <li>apply <code>literal_eval</code>, because explode doesn't work on <code>str</code> type</li>
  <li>explode the column to separate the <code>dicts</code> to separate rows</li>
  <li>normalize the column</li>
  </ul>
  </li>
  </ul>
  <pre class="lang-py prettyprint-override"><code>df = pd.DataFrame({'col_str': ['[{&quot;a&quot;: &quot;46&quot;, &quot;b&quot;: &quot;3&quot;, &quot;c&quot;: &quot;12&quot;}, {&quot;b&quot;: &quot;2&quot;, &quot;c&quot;: &quot;7&quot;}]', '[{&quot;b&quot;: &quot;2&quot;, &quot;c&quot;: &quot;7&quot;}, {&quot;c&quot;: &quot;11&quot;}]', np.nan]})
  
                                                      col_str
  0  [{&quot;a&quot;: &quot;46&quot;, &quot;b&quot;: &quot;3&quot;, &quot;c&quot;: &quot;12&quot;}, {&quot;b&quot;: &quot;2&quot;, &quot;c&quot;: &quot;7&quot;}]
  1                       [{&quot;b&quot;: &quot;2&quot;, &quot;c&quot;: &quot;7&quot;}, {&quot;c&quot;: &quot;11&quot;}]
  2                                                       NaN
  
  type(df.iloc[0, 0])
  [out]: str
      
  df.col_str.apply(literal_eval)
  </code></pre>
  <p>Error:</p>
  <pre class="lang-py prettyprint-override"><code>df.col_str.apply(literal_eval) results in ValueError: malformed node or string: nan
  </code></pre>