Python list comprehension to populate 2D array from excel data only gives 1 column -
python 3.5 on windows. have 9x2 range in excel want read array in python.
excel range data:
2 2 0.833333333 1 2.166666667 2 0 0 1 0 1 1 1.5 1.166666667 0.833 1.333 1.667 1.333
python code:
import openpyxl import numpy # physicians physicians = ['dra', 'drb'] # activities activities = ['admin1', 'frac2', 'spec3', 'fleb4', 'latr5', 'endo6', 'surg7', 'surg8', 'noth9', 'annl10'] wb = openpyxl.load_workbook('m_a_j2pasted.xlsx') type = wb sheet = wb.get_sheet_by_name('sheet_mean_a_j') m = [[sheet.cell(row=rows, column=cols).value rows in range(1, len(activities))] cols in range(1, len(physicians))] m = numpy.array(m) numpy.transpose(m) print(m) print(m) out: [[ 2. ] [ 0.83333333] [ 2.16666667] [ 0. ] [ 0.5 ] [ 0.5 ] [ 1.5 ] [ 0.83333333] [ 1.66666667]]
now how 2nd column in there? i've tried way:
m = [] rows in range(1, len(activities)): values = [] cols in range(1, len(physicians)): values.append(sheet.cell(row=rows, column=cols).value) m.append(values)
same thing.
thanks
you trying index uninitialized array first initialize it
w, h = 8, 5. matrix = [[0 x in range(w)] y in range(h)] #initializing
matrix[0][6] = 3 #input value or values through loop
print matrix[0][6] #prints element or elements
in case initialize dra,drb = 50,50. physicians = ['dra', 'drb']
read list comprehension in python
Comments
Post a Comment