I have a csv
file with headers like:
Given this test.csv
file:
"A","B","C","D","E","F","timestamp"
611.88243,9089.5601,5133.0,864.07514,1715.37476,765.22777,1.291111964948E12
611.88243,9089.5601,5133.0,864.07514,1715.37476,765.22777,1.291113113366E12
611.88243,9089.5601,5133.0,864.07514,1715.37476,765.22777,1.291120650486E12
I simply want to load it as a matrix/ndarray with 3 rows and 7 columns and also I want to access the column vectors
from a given column name
. If I use genfromtxt
(like shown below) I get an ndarray with 3 rows (one per line) and no columns.
r = np.genfromtxt('test.csv',delimiter=',',dtype=None, names=True)
print r
print r.shape
[ (611.88243, 9089.5601000000006, 5133.0, 864.07514000000003, 1715.3747599999999, 765.22776999999996, 1291111964948.0)
(611.88243, 9089.5601000000006, 5133.0, 864.07514000000003, 1715.3747599999999, 765.22776999999996, 1291113113366.0)
(611.88243, 9089.5601000000006, 5133.0, 864.07514000000003, 1715.3747599999999, 765.22776999999996, 1291120650486.0)]
(3,)
I can get column vectors from column names like this:
print r['A']
[ 611.88243 611.88243 611.88243]
If, I use load.txt
then I get the array with 3 rows and 7 columns but cannot access columns
by using the column
names (like shown below).
numpy.loadtxt(open("test.csv","rb"),delimiter=",",skiprows=1)
I get
[ [611.88243,9089.5601,5133.0,864.07514,1715.37476,765.22777,1.291111964948E12]
[611.88243,9089.5601,5133.0,864.07514,1715.37476,765.22777,1.291113113366E12]
[611.88243,9089.5601,5133.0,864.07514,1715.37476,765.22777,1.291120650486E12] ]
Is there any approach in Python
that I can achieve both the requirements together (access columns by coluumn name like np.genfromtext and have a matrix like np.loadtxt
)?