code: def flatten(rho, pval, sortby="cor"):
      n = rho.shape[0]
      idx = np.triu_indices(n, k=1)
      tab = pd.DataFrame(
          columns=['i', 'j', 'cor', 'pval'],
          data=np.c_[idx[0], idx[1], rho[idx], pval[idx]])
      tab[['i', "j"]] = tab[['i', "j"]].astype(int)
      if sortby == "cor":
          tab['abscor'] = np.abs(tab['cor'])
          tab.sort_values(by='abscor', inplace=True, ascending=False)
      elif sortby == "pval":
          tab.sort_values(by='pval', inplace=True, ascending=True)
      return tab[["i", "j", "cor", "pval"]]