Create a matrix from a text file - python -


i create matrix 3 column file. sure it's extremely easy, not understand how needs done. please gentle, beginner python. thank you

the format of input file

a 5  b 4  c 3  b b 2  b c 1  c c 0 

desired output - complete matrix

  b c 5 4 3 b 4 2 1 c 3 1 0 

or - half matrix

  b c 5 4 3 b   2 1 c     0 

i tried this, said, new python , programming.

import numpy np  line in file('test').readlines():     name1, name2, value = line.strip().split('\t')  = np.matrix([[name1], [name2], [value]]) print 

working script - 1 of friend helped me, if if interested in simpler script, here is. it's not efficient, works perfectly.

data = {} names = set([])  line in file('test').readlines():     name1, name2, value = line.strip().split('\t')     data[(name1, name2)] = value     names.update([name1])  names = sorted(list(names)) print  names print data  output = open('out.txt', 'w')  output.write("\t%s\n" % ("\t".join(names))) namea in names:     output.write("%s" % namea)     nameb in names:         key = (namea, nameb)         if key in data:             output.write("\t%s" % data[(namea, nameb)])          else:             output.write("\t")       output.write("\n")   output.close()  

try:

import pandas pd import numpy np  raw = [] open('test.txt','r') f:     line in f:         raw.append(line.split()) data = pd.dataframe(raw,columns = ['row','column','value']) data_ind = data.set_index(['row','column']).unstack('column') np.array(data_ind.values,dtype=float)) 

output:

array([[ 5., 4., 3.], [ nan, 2., 1.], [ nan, nan, 0.]])


Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -