Reputation: 863
I need to convert a pandas df to np array, but I lose the column names in the process. I have read and googled for a while now and I'm stuck.
My df looks like this:
mont_data.tail()
Out[114]:
Index MOEX.ME ^GSPC ^MXX ^N225 ^OSEAX
Date
2016-05-31 0.001482 -0.004077 0.002506 -0.000005 0.033240
2016-06-30 0.074850 0.008136 -0.002262 -0.029029 0.002518
2016-07-31 0.025882 0.030147 0.024242 0.007169 0.032473
2016-08-31 0.059069 0.014333 0.025050 0.025243 -0.008767
2016-09-30 0.035201 -0.009029 -0.016499 -0.039833 -0.003520
I then do:
mont_arr = mont_data.as_matrix(columns=[mont_data.columns[0:5]])
or:
mont_arr = mont_data.as_matrix(columns=[mont_data.columns[0:]])
Both result in a np array without column headers. Any idea how to solve this?
Using latest version of mentioned packages with Python 3.5
Upvotes: 1
Views: 1733
Reputation: 210922
you can do it this way:
In [19]: df
Out[19]:
MOEX.ME ^GSPC ^MXX ^N225 ^OSEAX
Date
2016-05-31 0.001482 -0.004077 0.002506 -0.000005 0.033240
2016-06-30 0.074850 0.008136 -0.002262 -0.029029 0.002518
2016-07-31 0.025882 0.030147 0.024242 0.007169 0.032473
2016-08-31 0.059069 0.014333 0.025050 0.025243 -0.008767
2016-09-30 0.035201 -0.009029 -0.016499 -0.039833 -0.003520
In [23]: result = pd.DataFrame(np.corrcoef(df), index=df.columns, columns=df.columns)
In [24]: result
Out[24]:
MOEX.ME ^GSPC ^MXX ^N225 ^OSEAX
MOEX.ME 1.000000 -0.087534 0.433865 -0.651299 0.093799
^GSPC -0.087534 1.000000 0.457119 0.657940 0.980054
^MXX 0.433865 0.457119 1.000000 -0.299503 0.596537
^N225 -0.651299 0.657940 -0.299503 1.000000 0.501435
^OSEAX 0.093799 0.980054 0.596537 0.501435 1.000000
Upvotes: 2
Reputation: 13913
You can compute the correlation coefficients between columns in pandas.
import io
import pandas as pd
data = io.StringIO('''\
Index MOEX.ME ^GSPC ^MXX ^N225 ^OSEAX
2016-05-31 0.001482 -0.004077 0.002506 -0.000005 0.033240
2016-06-30 0.074850 0.008136 -0.002262 -0.029029 0.002518
2016-07-31 0.025882 0.030147 0.024242 0.007169 0.032473
2016-08-31 0.059069 0.014333 0.025050 0.025243 -0.008767
2016-09-30 0.035201 -0.009029 -0.016499 -0.039833 -0.003520
''')
mont_data = pd.read_csv(data, delim_whitespace=True).set_index('Date')
print(mont_data.corr())
Output:
MOEX.ME ^GSPC ^MXX ^N225 ^OSEAX
MOEX.ME 1.000000 0.201809 0.030481 -0.152252 -0.762061
^GSPC 0.201809 1.000000 0.853232 0.595998 0.261402
^MXX 0.030481 0.853232 1.000000 0.926106 0.231001
^N225 -0.152252 0.595998 0.926106 1.000000 0.225621
^OSEAX -0.762061 0.261402 0.231001 0.225621 1.000000
Upvotes: 3