The following script converts multiple square matrices into pandas DataFrames, first manually, then as a loop function, plus labels their rows and columns identically.
import numpy as np
import pandas as pd
from numpy.random import rand
A1 = rand(3,3)
A2 = A1*2
A3 = A1*3 #example square matrices, no math pattern implied
labels = ['a','b','c']
Manual conversion:
A1 = pd.DataFrame(A1, index=labels, columns=labels)
A2 = pd.DataFrame(A2, index=labels, columns=labels)
A3 = pd.DataFrame(A2, index=labels, columns=labels)
Batch conversion (loop):
def batch_df(l):
ll = []
for A in l:
A_ = pd.DataFrame(A, index=labels, columns=labels)
ll.append(A_)
return ll
ll = batch_df([A1, A2, A3])
print(len(ll)) #list of unnamed matrices, but I would like them separately : A1, A2, ...
I would like to return the individual DataFrames separately instead, uniquely named, not bunched together in one list without names, but don't know how:
return A1, A2, A3
The function should be flexible enough to batch convert whatever number of arrays is passed into the function, not just 3