Home > Software engineering >  Compute correlation matrices of dataset list
Compute correlation matrices of dataset list

Time:09-17

I have a list of pandas datasets like the one below:

dataset_list=[df1,df2,df3]

What I need to do is compute the correlation coefficient of each dataset. Since I would not like to do this manually, I tried to use a for loop to accomplish this:

for dataset in dataset_list:
    corr_matrices=dataset.corr()
    corr_matrices.to_csv(str(dataset) 'Correlation_Matrices.csv', sep=',')

However, the output only shows the correlation matrix for the last dataset and not the first two. How can I fix this and save each correlation matrix in its own separate .csv file?

CodePudding user response:

You have to store each output. Here corr_matrices is just a variable inside the loop. Try this one

dataset_list=[df1,df2,df3]

output_list = list()
for i,dataset in enumerate(dataset_list):
    corr_matrices=dataset.corr()
    output_list.append(corr_matrices)
    dataset.to_csv(str(i) '_Correlation_Matrices.csv', sep=",")

for i in output_list:
    display(i)
  • Related