Pandas: Drop quasi-duplicates by column values
<p>I have a list that, let's say, looks like this (which I'm putting into a DF):</p>

<pre><code>[
['john', '1', '1', '2016'],
['john', '1', '10', '2016'],
['sally', '3', '5', '2016'],
['sally', '4', '1', '2016']
]
</code></pre>

<p><code>columns</code> are <code>['name', 'month', 'day', 'year']</code></p>

<p>I basically want to output a new DF with only the oldest row for each person. So it should contain two rows, one for john on 1/1/16 and one for sally on 3/5/16.</p>

<p>I have always had a tough time with this sort of selection inside DF's and was hoping someone could offer some advice on how to accomplish the above.</p>

<p>You can sort the data frame by <code>year, month, day</code> and then take the first row from each <code>name</code>:</p>

<pre><code>df.sort_values(by = ['year', 'month', 'day']).groupby('name').first()

#   month   day year
# name          
# john  1   1   2016
#sally  3   5   2016
</code></pre>

<p><em>Data</em>:</p>

<pre><code>df = pd.DataFrame([['john', '1', '1', '2016'],
                   ['john', '1', '10', '2016'],
                   ['sally', '3', '5', '2016'],
                   ['sally', '4', '1', '2016']], 
                   columns = ['name', 'month', 'day', 'year'])
</code></pre>

python pandas