I've been strugling to create a sub-array from specific elements of a first array.
Given a first array that looks like this (it commes from a txt file with two lines :
L1,(B:A:3:1),(A:C:5:2),(C:D:2:3)
L2,(C:E:2:0.5),(E:F:10:1),(F:D:0.5:0.5)):
code
toto = pd.read_csv("bd_2_test.txt",delimiter=",",header=None,names=["Line","1rst","2nd","3rd"])
matrix_toto = toto.values
matrix_toto
result
Line 1rst 2nd 3rd
0 L1 (B:A:3:1) (A:C:5:2) (C:D:2:3)
1 L2 (C:E:2:0.5) (E:F:10:1) (F:D:0.5:0.5)
how can I transform it into an array like this one?
array([['B', 'A', 3, 1],
['A', 'C', 5, 2],
['C', 'D', 2, 3],
['C', 'E', 2, 0.5],
['E', 'F', 10, 1],
['F', 'D', 0.5, 0.5]], dtype=object)
I tried vectorizing but I get each second element of the array.
np.vectorize(lambda s: s[1])(matrice_toto)
array([['1', 'B', 'A', 'C'],
['2', 'C', 'E', 'F']], dtype='<U1')
(B:A:3:1)in the first array. Are they tuples? Strings?valuesarray is a (2,4) array of strings. Yourlambdajust takes the 2nd character from each string. So for one thing you need to skip the "L1" element. You also need to split the other strings into 4 characters - drop the () and split on :. I don't know ifpandashas a 'expand' that will help. But at the numpy level you have a lot string manipulation to do first.