Transforms dataframe to properly formatted json response

def to_json(df, columns, confidence={}):
        """Transforms dataframe to properly formatted json response"""
        records = []

        display_cols = list(columns.keys())
        if not display_cols:
            display_cols = list(df.columns)

        bounds = {}
        for c in confidence:
            bounds[c] = {
                "min": df[confidence[c]["lower"]].min(),
                "max": df[confidence[c]["upper"]].max(),
                "lower": confidence[c]["lower"],
                "upper": confidence[c]["upper"]
            }

        labels = {}
        for c in display_cols:
            if "label" in columns[c]:
                labels[c] = columns[c]["label"]
            else:
                labels[c] = c

        for i, row in df.iterrows():
            row_ = DataTable.format_row(row, bounds, columns)
            records.append({labels[c]: row_[c] for c in display_cols})

        return {
            "data": records,
            "columns": [{"data": labels[c]} for c in display_cols]
        }