title: Python: Groupby First Non NaN Value
body: <p>I have the following dataframe:</p>
  
  <pre><code>id    number   
  1      13
  1      13
  1      NaN
  1      NaN
  2      11
  2      11
  2      11
  2      NaN
  </code></pre>
  
  <p>I want to find the first non-NaN value per id and mark it with a 1. The result should look like this:</p>
  
  <pre><code>id    number     code   
  1      13         NaN
  1      13         1
  1      NaN        NaN
  1      NaN        NaN
  2      11         NaN
  2      11         NaN
  2      11         1
  2      NaN        NaN
  </code></pre>
  
  <p>I tried the following command and then go from there:</p>
  
  <pre><code>df["test"] = df.groupby("id")["number"].first_valid_index()
  </code></pre>
  
  <p>It gives me the following error: Cannot access callable attribute 'first_valid_index' of 'SeriesGroupBy' objects, try using the 'apply' method</p>
  
  <p>Then I tried this:</p>
  
  <pre><code>df['test'] = df.groupby("id")['number'].apply(lambda x: x.first_valid_index())
  </code></pre>
  
  <p>But this gives me just a column of Nats... </p>
  
  <p>Does anybody know how the problem could be solved efficiently?</p>