1

I have this code:

path = "C:\\Files"
dir = os.listdir(path)
diccio = {}
list = []
MValores = np.array([])
for i in dir:
    file = np.genfromtxt(os.path.join(path, i), delimiter=",")
    precioCierre = file[:, 0]
    list.append(precioCierre)
    nombre = i[1:7]
    diccio[nombre] = precioCierre

 MValores = np.column_stack(list)
 data = pd.DataFrame(diccio)

I have a list of ".csv" files.

-EURHKD_H1.csv
-EURJPY_H1.csv
-EURNZD_H1.csv
-EURTRY_H1.csv
-EURUSD_H1.csv

And i use this code to create a DataFrame. With this df i do this:

data = np.corrcoef(data, rowvar=0)
A = minimum_spanning_tree(data)
print(A)
print(type(data))

the problem is when i get the minimun spanning tree i get a numpy type

<class 'numpy.ndarray'>

and i wanna get a DataFrame type. With the same code i use with numpy i wanna convert that code to pandas code to create a DataFrame.

Cristoff
  • 149
  • 6
  • It's maybe a duplicate from this post : [Creating a Pandas DataFrame from a Numpy array: How do I specify the index column and column headers?](http://stackoverflow.com/questions/20763012/creating-a-pandas-dataframe-from-a-numpy-array-how-do-i-specify-the-index-colum) – Essex Apr 27 '16 at 00:38

1 Answers1

0

DataFrame.corr is the Pandas equivalent of np.corrcoef(..., rowvar=0).

import numpy as np
import pandas as pd

ar = np.random.randn(100, 3)
df = pd.DataFrame(ar)

assert np.allclose(np.corrcoef(ar, rowvar=0), df.corr().values)

Note that to get back a DataFrame, minimum_spanning_tree must also work with DataFrame objects.

Alternatively, you can create a DataFrame from the result of minumum_spanning_tree.

DataFrame(minimum_spanning_tree(np.corrcoef(data, rowvar=0)),
          columns=data.columns, index=data.columns)
Igor Raush
  • 15,080
  • 1
  • 34
  • 55