gffromtxt question (mixed format - string, float)

Asked 2 years ago, Updated 2 years ago, 42 views

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

2022-09-21 17:12

1 Answers

(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.

Note: https://stackoverflow.com/questions/9534408/numpy-genfromtxt-produces-array-of-what-looks-like-tuples-not-a-2d-array-why

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:])


2022-09-21 17:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.