code: def prepare_stats(stats, recorded_cols=list()):
      """
      Prepare the stats DataFrame for user-friendly output.
  
      Parameters
      ----------
      stats: list[Object]
      recorded_cols: list[str]
  
      Returns
      -------
  
      """
      asset_cols = list()
  
      stats = copy.deepcopy(stats)
      # Using a copy since we are adding rows inside the loop.
      for row_index, row_data in enumerate(list(stats)):
          assets = [p["sid"] for p in row_data["positions"]]
  
          asset_values = dict()
          if recorded_cols is not None:
              for column in recorded_cols[:]:
                  value = row_data[column]
                  if isinstance(value, pd.Series):
                      value = value.to_dict()
  
                  if type(value) is dict:
                      for asset in value:
                          if not isinstance(asset, TradingPair):
                              break
  
                          if asset not in assets:
                              assets.append(asset)
  
                          if asset not in asset_values:
                              asset_values[asset] = dict()
  
                          asset_values[asset][column] = value[asset]
  
          if len(assets) == 1:
              row = stats[row_index]
              asset_cols = set_position_row(
                  row, assets[0], asset_values
              )
  
          elif len(assets) > 1:
              for asset_index, asset in enumerate(assets):
                  if asset_index > 0:
                      row = copy.deepcopy(row_data)
                      stats.append(row)
  
                  else:
                      row = stats[row_index]
  
                  asset_cols = set_position_row(
                      row, assets[asset_index], asset_values
                  )
  
      df = pd.DataFrame(stats)
      df["orders"] = df["orders"].apply(lambda orders: len(orders))
      df["transactions"] = df["transactions"].apply(
          lambda transactions: len(transactions)
      )
      index_cols = [
          "period_close",
          "starting_cash",
          "ending_cash",
          "portfolio_value",
          "pnl",
          "long_exposure",
          "short_exposure",
          "orders",
          "transactions",
      ]
  
      # Removing the asset specific entries
      if recorded_cols is not None:
          recorded_cols = [
              x for x in recorded_cols if x not in asset_cols
          ]
          for column in recorded_cols:
              index_cols.append(column)
  
      if asset_cols:
          columns = asset_cols
          df.set_index(index_cols, drop=True, inplace=True)
  
      else:
          columns = index_cols
          columns.remove("period_close")
          df.set_index("period_close", drop=False, inplace=True)
  
      df.dropna(axis=1, how="all", inplace=True)
      df.sort_index(axis=0, level=0, inplace=True)
  
      return df, columns
  
imports: import copy
  import csv
  import json
  import numbers
  import os
  import time
  import numpy
  import pandas
  from catalyst.assets._assets import TradingPair
  from catalyst.exchange.utils.exchange_utils import get_algo_folder
  from catalyst.utils.paths import data_root, ensure_directory
  from operator import itemgetter
label_desc: Use image node_id 6 for calling a global function with example usage: prepare_stats(stats, recorded_cols) and returns: df,  columns