<html><head><meta http-equiv="Content-Type" content="text/html charset=iso-8859-1"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Ah yes I see it. I forgot you can pass around functions in python.<div><br></div><div>Thanks for the help,</div><div>Mike</div><div><br></div><div><br></div><div><br><div><div>On Dec 13, 2013, at 9:29 PM, Amit Saha <<a href="mailto:amitsaha.in@gmail.com">amitsaha.in@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">On Sat, Dec 14, 2013 at 12:14 PM, Michael Crawford <<a href="mailto:dalupus@gmail.com">dalupus@gmail.com</a>> wrote:<br><blockquote type="cite">I found this piece of code on github<br><br><a href="https://gist.github.com/kljensen/5452382">https://gist.github.com/kljensen/5452382</a><br><br>def one_hot_dataframe(data, cols, replace=False):<br> """ Takes a dataframe and a list of columns that need to be encoded.<br> Returns a 3-tuple comprising the data, the vectorized data,<br> and the fitted vectorizor.<br> """<br> vec = DictVectorizer()<br> mkdict = lambda row: dict((col, row[col]) for col in cols)<br>#<<<<<<<<<<<<<<<<<<<br> vecData = pandas.DataFrame(vec.fit_transform(data[cols].apply(mkdict,<br>axis=1)).toarray())<br> vecData.columns = vec.get_feature_names()<br> vecData.index = data.index<br> if replace is True:<br> data = data.drop(cols, axis=1)<br> data = data.join(vecData)<br> return (data, vecData, vec)<br><br>I don't understand how that lambda expression works.<br>For starters where did row come from?<br>How did it know it was working on data?<br></blockquote><br>Consider this simple example:<br><br><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">l = lambda x: x**2<br>apply(l, (3,))<br></blockquote></blockquote></blockquote>9<br><br>A lambda is an anonymous function. So, when you use apply(), the<br>lambda, l gets the value 3 in x and then returns x**2 which is 9 in<br>this case.<br><br>Hope this helps you.<br><br>Best,<br>Amit.<br></blockquote></div><br></div></body></html>