i have data frame needs re-represented. original data frame has each row unique search term , columns resulting products. each row different length.
i want turn rectangular dataframe (called rectangle in code below) rows still unique search terms, column headers unique products. each element 1 or 0 represent whether product result of search term.
here have:
ashwiniinput = pd.read_csv('c:/users/.../ashwiniinputdata.csv') ashwiniinput = ashwiniinput.set_index(keys='search_term_orig',drop = true,verify_integrity=true ) #get unique products list allproducts = pd.unique(ashwiniinput.values.ravel()) allproducts = [str(product) product in (allproducts)] def makebooldictofsearchtermsandproducts(term, productsrelatedtoterm, allproducts): """ use on each search term in ashwiniinput dict 1 or 0 each product """ returndict = {} product in allproducts: if product in productsrelatedtoterm: returndict[product] = 1 else: returndict[product] = 0 return term, returndict rectangle = pd.dataframe(np.zeros(shape = (len(ashwiniinput.index),len(allproducts))), index = ashwiniinput.index, columns = allproducts)
how iterate on each row , column in rectangle
run function makebooldictofsearchtermsandproducts()
on , fill in correct element result? should use apply? or map? or perhaps apply_map?
generally, pandas dataframe if want iterate on rows , treat each row vector. suggest use 2-dimensional numpy array. once have array, can iterate on each row , columns easily. here sample code:
`for index, value in ndenumerate( self.cells ): do_something( value ) self.cells[index] = new_value`