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><strong><em>Option 1</em></strong><br>
use <code>pd.to_datetime</code> to parse ['year', 'month', 'day'] columns.<br>
<code>groupby('name')</code> then take <code>first</code></p>

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

<p><a href="http://i.stack.imgur.com/eDHDv.png" rel="nofollow"><img src="http://i.stack.imgur.com/eDHDv.png" alt="enter image description here"></a></p>

<p><strong><em>Option 2</em></strong><br>
Same <code>pd.to_datetime</code> usage.<br>
<code>groupby('name')</code> take <code>idxmin</code> to locate smallest date.</p>

<pre><code>df['date'] = pd.to_datetime(df[['year', 'month', 'day']])
df.ix[df.groupby('name').date.idxmin()]
</code></pre>

<p><a href="http://i.stack.imgur.com/Julzk.png" rel="nofollow"><img src="http://i.stack.imgur.com/Julzk.png" alt="enter image description here"></a></p>

python pandas