title: Python: How to access returned values to put into a table?
body: <p><em>Note: I am a very beginner in Python so please bear with me!</em></p>
  
  <p><em>edit: I have fixed the error but I need help with my question down below!</em></p>
  
  <p><strong>My question is:</strong></p>
  
  <p>1) If I want to put the minimum value(s) and most common word/number onto a table, how can I index to the minimum value/most common word and extract it and place it in the correct position on my table?</p>
  
  <p><strong>Description</strong></p>
  
  <p>The following code is supposed to transpose the given nested list, A, using the function </p>
  
  <p><code>def rows2cols(A):</code>, </p>
  
  <p>then run through that list and for every column I check to see if it has numeric values or not using </p>
  
  <p><code>def isnumericlist(A):</code>. </p>
  
  <p>If the list does have numeric values, I convert the strings to floats and find the minimum value and most common word/number from that list.</p>
  
  <p>The code is the following:</p>
  
  <pre><code>A = [['OrderDate', 'Region', 'Rep', 'Item', 'Units', 'Unit Price'],['4-Jul-2014', 'East', 'Richard', 'Pen Set', '62', '4.99'], ['12-Jul-2014', 'East', 'Nick', 'Binder', '29', '1.99'], ['21-Jul-2014', 'Central', 'Morgan', 'Pen Set', '55', '12.49'], ['29-Jul-2014', 'East', 'Susan', 'Binder', '81', '19.99'],['7-Aug-2014', 'Central', 'Matthew', 'Pen Set', '42', '23.95'], ['15-Aug-2014', 'East', 'Richard', 'Pencil', '35', '4.99'], ['24-Aug-2014', 'West', 'James', 'Desk', '3', '275']]
  
  minVal = []
  maxVal = []
  hist = []
  average = []
  stanDev = []
  headers = A[0] #this sets the variable "headers" as the first row 
  rows = A[1:] #skips the first row
  
  def rows2cols(A):
      if len(A) == 0: 
          return []                      #this covers the base case of having an empty csv file
      res  = [[] for x in headers]       # would create a list of empty lists
      for line in A: 
          for col in range(len(line)): 
              res[col].append(line[col]) 
      return res
  
  def convertstringtofloats(A):
      res = []
      for x in A:
          res.append(float(x))
      return res
  
  def isnumericlist(A):
      for x in A:
          try:
              numeric = float(x) 
          except:
              return False
      return True
  
  
  def getMin(A):
      res = B[0] #first column AFTER you transpose the nested list
      for x in A:
          if x &lt; res:
              res = x
      return res
  
  def most_common(A):
      counts = {}
      for x in A:
          counts[tuple(x)] = counts.get(tuple(x), 0) + 1 
      max = -1
      maxKey = ""
      for key,value in counts.items():
          if max &lt; value:
              max = value
              maxKey = key
      return maxKey
  
  def notnumeric(A):
      return "n/a"
  
  cols = rows2cols(rows)
  
  for col in range(len(headers)):
      if isnumericlist(cols[col]):
          B = convertstringtofloats(cols[col])
          minVal.append(getMin(B))
          maxVal.append(getMax(B))
          average.append(getAvg(B))
          stanDev.append(getSD(B))
      else:
          notnumeric(col)
  
      mode.append(most_common(cols[col]))
  
  tablevalues = [minVal, maxVal, average, stanDev, mode]
  </code></pre>
  
  <p><strong>My code for producing a table is below, along with a sample table as to how I would like the outcome to be!</strong></p>
  
  <pre><code>def print_table(table):
      longest_cols = [
          (max([len(str(row[i])) for row in table]) + 0) for i in range(len(table[0]))
      ]
      row_format = "|".join([" {:&gt;" + str(longest_col) + "} " for longest_col in longest_cols])
      first = True
      for row in table:
          print(row_format.format(*row))
          if first:
              print((sum(longest_cols) + (len(table[0]) - 0) * 3) * "-")
              first = False
  
  table = [
      ["Columns:", "Min", "Max", "Avg", "Std. Dev.", "Most Common Word"],
      ["OrderDate", "n/a", "n/a", "n/a", "n/a", "John"],
      ["Region", 3.3, 6.29, 4.888, 1.333, 4.911],
      ["Rep", 1.3, 3.2, 1.888, 0.333, 1.9],
      ["Item", 1.3, 3.2, 1.888, 0.333, 1.9],
      ["Units","n/a", "n/a", "n/a", "n/a", "John"],
      ["Unit Price","n/a", "n/a", "n/a", "n/a", "John"]
  ]
  print_table(table)
  </code></pre>