0

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

1 Answer 1

1
   
   def batch_df(l):

       ...

       return ll

   # edit the expected return values
   A1, A2, A3 = batch_df([A1, A2, A3])

(i don't really understand your question)

or


import numpy as np
import pandas as pd
from numpy.random import rand

my_dfs = {}
my_dfs['A1'] = rand(3,3)
my_dfs['A2'] = my_dfs['A1'] * 2
my_dfs['A3'] = my_dfs['A1'] * 3 

labels = ['a','b','c']

#----------#
# Do stuff #
#----------#
def batch_df(d):
    
    # loop over the dataframes
    for k, A in d.items():
        # update the value
        my_dfs[k] = pd.DataFrame(A, index=labels, columns=labels)

     # no need to return --> dictionaries are by reference
        
        
batch_df(my_dfs)

res: my_dfs['A1']

    a           b           c
a   0.501693    0.506768    0.315132
b   0.382843    0.116506    0.194385
c   0.208627    0.033067    0.121261
Sign up to request clarification or add additional context in comments.

7 Comments

ll in the question is a list. but inside that list are separate objects. making a function output ll by itself gives only one output. I instead would like to output 3 or more objects from the function, unpacked from the contents of ll
but if you provide an equal amount of expected values --> A1, A2, A3 = <-- then there is no real issue?
or that amount is unknown?
Why don't you use, a dictionary?
the code before the edits worked fine, i was only replying to your question about not understanding
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.