title: use .assign() method with lambda in python
body: <p>I run this code in Python:</p>
  
  <pre><code> #Declaring these now for later use in the plots
   TOP_CAP_TITLE = 'Top 10 market capitalization'
   TOP_CAP_YLABEL = '% of total cap'
  
   # Selecting the first 10 rows and setting the index
   cap10 = cap.loc[:10, :].set_index('id')
  
   # Calculating market_cap_perc
   cap10 = cap10.assign(market_cap_perc =
        lambda x: (x.market_cap_usd / cap.market_cap_usd.sum()) * 100)
   # Plotting the barplot with the title defined above 
   ax = cap10.plot.bar(x= id, y= market_cap_perc)
   ax.set_title(TOP_CAP_TITLE)
   # Annotating the y axis with the label defined above
   ax.set_ylabel(TOP_CAP_YLABEL)
  </code></pre>
  
  <p>And getting an error:</p>
  
  <pre><code>NameError Traceback (most recent call last) in ()
     10 lambda x: (x.market_cap_usd / cap.market_cap_usd.sum()) * 100) 
     11 # Plotting the barplot with the title defined above ---&gt; 
     12 ax = cap10.plot.bar(x= id, y= market_cap_perc) 
     13 ax.set_title(TOP_CAP_TITLE) 
     14 # Annotating the y axis with the label defined above
  NameError: name 'market_cap_perc' is not defined
  </code></pre>
  
  <p>Thats code from Task4 on DataCamp Project Exploring the Bitcoin cryptocurrency market. <code>cap</code> is DataFrame with column <code>id</code>(such as 'bitcoin', 'ripple'). Another column <code>market_cap_usd</code> (this column consist cost of cryptocurrency market in usd. For example, '159640995719' - its a <code>market_cap_usd</code> for bitcoin). 
  There are instructions to complete this task:</p>
  
  <p>1.Select the first 10 coins, set the index to <code>id</code>, and assign the resulting DataFrame to <code>cap10</code>. </p>
  
  <p>2.Calculate the percentage of market capitalization for each coin using <code>assign()</code> and assign it to <code>cap10</code> again.</p>
  
  <p>3.Plot the top 10 coin's <code>market_cap_perc</code> in a barplot with the title "Top 10 market capitalization" and assign it to <code>ax</code>.</p>
  
  <p>4.Using the <code>ax</code> object, annotate the y axis with "% of total cap".</p>
  
  <p>I try to define market_cap_perc before lambda:
  <code>market_cap_perc = 0</code>
  and get an error:</p>
  
  <pre><code>KeyError                                  Traceback (most recent call last)
  
      2133             try:
  -&gt;  2134                 return self._engine.get_loc(key)
      2135             except KeyError:
  
  pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4443)()
  
  pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4289)()
  
  KeyError: 139887424317984
  </code></pre>