title: Pandas apply method: get an index label from a pivot_table
body: <p>I am using a well-known dataset as an example. These are the most popular baby names given to a newborn baby in New York City, based on ethnicity. This well-known dataset is available at this address:&quot;https://data.cityofnewyork.us/api/views/25th-nujf/rows.csv&quot;</p>
  <p>I have been using Pandas for a few months, and still have issues with the pivot_table.
  I wanted to know for each year, what is the most popular first name, and I did this (it works):</p>
  <pre><code>import pandas as pd
  url = &quot;https://data.cityofnewyork.us/api/views/25th-nujf/rows.csv&quot;
  df = pd.read_csv (url)
  pt = df.pivot_table (index = &quot;Child's First Name&quot;, columns = &quot;Year of Birth&quot;, values ​​= &quot;Gender&quot;, aggfunc = &quot;count&quot;, fill_value = 0, margins = True)
  </code></pre>
  <p>The variable pt gives me the list of first names, in line, and in column the years. And the values ​​are the number of times the first name has been given for a year.</p>
  <p>Now, I want to do the opposite: from a value over one year, find the corresponding index (therefore the first name).
  For example, I want to know which first names were given 4 times in 2015.
  To do this I do:</p>
  <pre><code>condition = pt [2015] == 4
  result = pt [condition]
  print(result)
  </code></pre>
  <p>Now I will want to use an apply function which will return me for each row, the first name in question.
  I did this, <strong>but it doesn't work</strong>:</p>
  <pre><code>pt [&quot;First Name First Letter&quot;] = pt.apply (lambda x: x.index [0], axis = 1)
  </code></pre>
  <p>I definitely want to use the apply function because I feel that there is always something that works differently when dealing with a pivot_table...
  Who could help me please ?
  <a href="https://i.stack.imgur.com/dBjYK.png" rel="nofollow noreferrer">Here is the false result I get</a></p>