It contains the following files (asset.csv):
import numpy as np
data_arr = np.genfromtxt('asset.csv',encoding='ascii' ,delimiter=',', dtype=None)
Use the corresponding code
array([('G1', 1, 100, 5, 0), ('G1', 1, 21, 538, 0), ('G1', 1, 22, 6000, 0), ..., ('G2', 8, 61, 241908, 8800),('G2', 8, 70, 57341, 16800), ('G2', 9, 51, 1340, 0)], dtype=[('f0', '<U2'), ('f1', '<i4'), ('f2', '<i4'), ('f3', '<i4'), ('f4', '<i4')])
That's what I got.
But I'm
array([['G1', 1, 100, 5, 0],['G1', 1, 21, 538, 0,...,['G2', 8, 70, 57341, 16800],['G2', 9, 51, 1340, 0]])
I want to get the result of a two-dimensional array like this.
What should I do?
python numpy
(2-d) nd.array must be of the same type for all elements. Now, if you look at the data, the first element of each row is a letter type, and the rest is a number type. I can't make it the same data type. So, it seems that the data in the form structured ndarray has been returned.
My recommendation is just to use pandas.read_csv
.
df=read_csv('source_dat.csv', header=None) #headerNone to avoid interpreting the first line as a column name
df.info()
Read it like this.
To read and replace only numeric parts with 2-nd.array
values = np.array(df.iloc[:,1:])
© 2024 OneMinuteCode. All rights reserved.