1

This is a continuation of an earlier learning on numpy arrays.
A structured array is created from the elements of a list - and thereafter populated with values(not shown below).

>>> o = ['x','y','z']
>>> import numpy as np
>>> b = np.zeros((len(o),), dtype=[(i,object) for i in o])
>>> b
array([(0, 0, 0, 0, 0), (0, 0, 0, 0, 0), (0, 0, 0, 0, 0)], 
      dtype=[('x', '|O4'), ('y', '|O4'), ('z', '|O4')])

The populated array looks as below:

    x   y   z   
x   0   1   0   
y   1   0   1,5 
z   0   1,5 0   

1.How do we add new vertices to the above?
2.Once the vertices have been added,what is the cleanest process to add the following array to the structured array(NOTE:not all vertices in this array are new):

    d    e      y
d   0   '1,2'   0
e  '1,2' 0     '1'
f   0   '1'     0

The expected output(please bear with me):

    x   y   z   d   e   f
x   0   1   0   0   0   0
y   1   0   1,5 0   1   0
z   0   1,5 0   0   0   0
d   0   0   0   0   1,2 0
e   0   1   0   1,2 0   0
f   0   0   0   0   1   0
IUnknown
  • 9,301
  • 15
  • 50
  • 76

1 Answers1

0

Seems like a job for python pandas.

>>> import numpy as np
>>> import pandas as pd
>>> data=np.zeros((4,5))
>>> df=pd.DataFrame(data,columns=['x','y','z','a','b'])
>>> df
   x  y  z  a  b
0  0  0  0  0  0
1  0  0  0  0  0
2  0  0  0  0  0
3  0  0  0  0  0
>>> df['c']=0  #Add a new column
>>> df
   x  y  z  a  b  c
0  0  0  0  0  0  0
1  0  0  0  0  0  0
2  0  0  0  0  0  0
3  0  0  0  0  0  0

>>> new_data=pd.DataFrame([['0','1,2','0'],['1,2','0','1'],['0','1','0']],columns=['d','e','y'])
>>> new_data
     d    e  y
0    0  1,2  0
1  1,2    0  1
2    0    1  0

>>> df.merge(new_data,how='outer') #Merge data
    x  y   z   a   b   c    d    e
0   0  0   0   0   0   0  NaN  NaN
1   0  0   0   0   0   0  NaN  NaN
2   0  0   0   0   0   0  NaN  NaN
3   0  0   0   0   0   0  NaN  NaN
4 NaN  0 NaN NaN NaN NaN    0  1,2
5 NaN  0 NaN NaN NaN NaN    0    1
6 NaN  1 NaN NaN NaN NaN  1,2    0

There are many ways to merge the data that you showed, can you please explain in more detail exactly what you would like the ending array to look like?

Daniel
  • 19,179
  • 7
  • 60
  • 74