'Python DATAFRAME add column as Matrix

I need to save matrix results in one dataFrame. to do that: i split matrix and i create a new dataFrameor each iteration and i append it to Target dataFrame. i don't know if is The good way or not what about perFormance?

import pandas as pd
import numpy as np


def generate_Matrix_as_dataframe( productname,variableName,results):


#    df_results = pd.DataFrame({'Values': result})
    df= pd.DataFrame(results)
    dimension = len(results[0])
    df['Values'] = pd.Series(df.fillna('').values.tolist())
    #    convert to Array
    df['Values'] = df['Values'].apply(lambda x: np.array(x))
    df_results =df[df.columns.drop([i for i in range(dimension)])]
    df_results = df_results.reset_index()
    df_results= df_results.rename(columns={"index":"Generation"})
    df_results['Depth'] = df_results.index + 1
    df_results['ProductName'] = productname
    df_results['VariableName'] = variableName
    return df_results[['ProductName','VariableName' ,'Depth', 'Values']]


df_results_ifrs17 = pd.DataFrame(columns=['ProductName', 'VariableName','Depth', 'Values'])
products =['P1','P2']
variables =['V1','V2']
nbrproduct=1
nbvariables=1
for p in products:
    for v in variables:
        value= np.ones( (nbrproduct, nbvariables), dtype=np.int32 )

        df_results = generate_Matrix_as_dataframe(p, v,value)
        df_results_ifrs17 = df_results_ifrs17.append(df_results, ignore_index=True)
        nbvariables=nbvariables+1
        print(value)
    nbrproduct=nbrproduct+1
print(df_results_ifrs17)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source