Issue
i have a table that looks like this
and i want to upload it to an array so the first colum would be city name and the first row will be the parameters. is it possible given different types? i need an array like this :
[[city name total 00-04 05-09]
[j 882806 110386 98268]
[a 221560 21982 19317]
[h 279591 21069 18200]]
when i use
csv = np.genfromtxt('populationData2016.csv',delimiter=",")
i get this
[[ nan nan nan ..., nan
nan nan]
[ nan 8.82806000e+05 1.10386000e+05 ..., 2.57210000e+04
1.77910000e+04 3.56080000e+04]
Solution
def load_table_as_array(filename):
f = open(filename,'r')
# Parsing the first line containing column headers
header_line = f.readline()
header_line = header_line.strip(',\n')
column_names = header_line.split(',’)
#Parsing the rest of the file
mat = []
row_names = []
for line in f:
tokens = line.rstrip().split(',')
row_names.append(tokens[0]) #Add first token to row header list
values = [float(n) for n in tokens[1:]] # Convert to float
mat.append(values) # Append the current row to the matrix
f.close()
row_names = np.array(row_names)
column_names = np.array(column_names)
data = np.array(mat)
return data, column_names, row_names
this works thank for who ever tried to help
Answered By - tt600
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.