def filter_fields(header, data, fields):
  """Filter (header, data) with selected header fields.

  Args:
    header ([str]): The header.
    data ([[float]]): The data, with the same number of columns as header.
    fields ([str]): The fields that need to be written.

  Returns:
    (header ([str]), data ([[float]]))

  Examples:
    >>> filter_fields(['a','b','c'], [[0,1,2], [3,4,5]], ['d','b','a'])
    (['b', 'a'], [[1, 0], [4, 3]])
  """

  picked = []
  for f in fields:
    try:
      picked.append(header.index(f))
    except ValueError:
      # OK to have missing values
      pass

  h = [header[i] for i in picked]
  d = []
  for e in data:
    d.append([e[i] for i in picked])
  return (h, d)