<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">This is exactly the problem that pandas DataFrames solve.<div class=""><br class=""></div><div class=""><br class=""></div><div class="">Initialization may be done in multiple ways:</div><div class=""><font face="Courier New" class=""><br class=""></font></div><div class=""><font face="Courier New" class="">>>> import pandas as pd</font></div><div class=""><font face="Courier New" class=""><br class=""></font></div><div class=""><font face="Courier New" class=""><br class=""></font></div><div class=""><div class=""><font face="Courier New" class="">>>> table = pd.DataFrame(</font></div><div class=""><font face="Courier New" class="">        {'forecast': {'occurred': 0, 'not_occurred': 0},</font></div><div class=""><font face="Courier New" class="">         'not_forecast': {'occurred': 0, 'not_occurred': 0}}</font><span style="font-family: 'Courier New';" class="">)</span></div></div><div class=""><div class=""><font face="Courier New" class=""><br class=""></font></div><div class=""><font face="Courier New" class="">>>> table = pd.DataFrame([[0, 0], [0, 0]],</font></div><div class=""><font face="Courier New" class="">                         columns=('forecast', 'not_forecast'),</font></div><div class=""><font face="Courier New" class="">                         index=('occurred', 'not_occurred’))</font></div></div><div class=""><font face="Courier New" class="">>>> table</font></div><div class=""><table class="dataframe" border="1"><thead class=""><tr style="text-align: right;" class=""><th class=""></th>
      <th class="">forecast</th>
      <th class="">not_forecast</th>
    </tr>
  </thead>
  <tbody class="">
    <tr class="">
      <th class="">not_occurred</th>
      <td class="">0</td>
      <td class="">0</td>
    </tr>
    <tr class="">
      <th class="">occurred</th>
      <td class="">0</td>
      <td class="">0</td></tr></tbody></table><div class=""><br class=""></div></div><div class=""><br class=""></div><div class="">It supports attribute or item syntax</div><div class=""><font face="Courier New" class=""><br class=""></font></div><div class=""><font face="Courier New" class="">>>> table.forecast is table['forecast']</font></div><div class=""><font face="Courier New" class="">True</font></div><div class=""><font face="Courier New" class="">>>> table.forecast</font></div><div class=""><div class="output_text output_result output_subarea"><pre class=""><font face="Courier New" class="">not occurred    0
occurred        0
Name: forecast, dtype: int64</font></pre><pre class=""><font face="Courier New" class=""><br class=""></font></pre></div></div><div class="">At this point ‘occurred’/‘not_occurred’ are the <i class="">index</i>, not an addressable <i class="">column.</i></div><div class="">Query them by taking transpose (.T) of table:</div><div class=""><br class=""></div><div class=""><font face="Courier New" class="">>>> table.T</font></div><table class="dataframe" border="1"><thead class=""><tr style="text-align: right;" class=""><th class=""></th>
      <th class="">not_occurred</th>
      <th class="">occurred</th>
    </tr>
  </thead>
  <tbody class="">
    <tr class="">
      <th class="">forecast</th>
      <td class="">0</td>
      <td class="">0</td>
    </tr>
    <tr class="">
      <th class="">not_forecast</th>
      <td class="">0</td>
      <td class="">0</td></tr></tbody></table><div class=""><br class=""></div><div class=""><font face="Courier New" class="">>>> table.T.not_occurred</font></div><div class=""><div class="output_text output_result output_subarea"><pre class=""><font face="Courier New" class="">forecast        0
not_forecast    0
Name: not_occurred, dtype: int64</font></pre></div><div class="">There is also the stack and unstack methods which linearizes the matrix, either</div><div class="">bringing ‘occurred’ up to columns or ‘forecast’ down to index.</div><div class=""><br class=""></div><div class="">You can create higher-dimentional frames with a MultiIndex for double columns</div><div class="">of forecast+occurred, and an index like time or location.</div><div class=""><br class=""></div><div class="">Good Luck</div><div class=""><br class=""></div><div class="">- Andrew Kubera</div><div class=""><br class=""></div><div class=""><br class=""></div><div><blockquote type="cite" class=""><div class="">On Dec 8, 2016, at 9:19 PM, Eric Floehr <<a href="mailto:eric@intellovations.com" class="">eric@intellovations.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class=""><div class=""><div class=""><div class=""><div class=""><div class=""><div class=""><div class=""><div class=""><div class=""><div class=""><div class=""><div class="">I was wondering if anyone knows of any way in Python (functional or not), or a module, that would allow the following:<br class=""><br class=""></div>Let's say I have an n-dimensional array, with keys (numeric or otherwise) for each dimension. I'd like to be able to set up and grab the elements in any row in any dimension equally easily.<br class=""><br class=""></div>Here's a simple example, a 2x2 contingency table, with rows labelled "forecast" and "not forecast" and columns labelled "occurred" and "not occurred". Within each of the 4 cells, there is some value.<br class=""><br class=""></div>If I set it up traditionally, as a dict within the dict:<br class=""><br class=""></div>table = {}<br class=""></div>table['forecast'] = {'occurred': 0, 'not occurred': 0}<br class=""></div>table['not forecast'] =  {'occurred': 0, 'not occurred': 0}<br class=""><br class=""></div>and access it traditionally:<br class=""><br class=""></div>table['forecast']['occurred'] = 123<br class=""><br class=""></div>The problems are:<br class=""><br class=""></div>1. Set up is hard... I have to duplicate keys, and in an n-dimensional array, this is a real pain.<br class=""><br class=""></div>2. I always have to access a cell in the right order, and have to remember that order. It would be great if I could access a cell in the example above as table['forecast']['occurred'] or table['occurred']['forecast'] (syntax doesn't matter, table('forecast', 'occurred') is fine too).<br class=""><br class=""></div>3. It's hard to slice... in a 2-dimensional array, it's easy to get the cells in the outermost dict via table['forecast'].values() but how would I just as easily (equivalently) get table['not occurred'].values()?<br class=""><br class=""></div>Thoughts? Am I missing something obvious?<br class=""><br class=""></div>
_______________________________________________<br class="">CentralOH mailing list<br class=""><a href="mailto:CentralOH@python.org" class="">CentralOH@python.org</a><br class="">https://mail.python.org/mailman/listinfo/centraloh<br class=""></div></blockquote></div><br class=""></div></body></html>