How to drop duplicate from DataFrame taking into account value of another column
<p>When I drop <code>John</code> as duplicate specifying 'name' as the column name: </p>

<pre><code>import pandas as pd   
data = {'name':['Bill','Steve','John','John','John'], 'age':[21,28,22,30,29]}
df = pd.DataFrame(data)
df = df.drop_duplicates('name')
</code></pre>

<p>pandas drops all matching entities leaving the left-most:</p>

<pre><code>   age   name
0   21   Bill
1   28  Steve
2   22   John
</code></pre>

<p>Instead I would like to keep the row where John's age is the highest (in this example it is the age 30. How to achieve this?</p>

<p>Try this:</p>

<pre><code>In [75]: df
Out[75]:
   age   name
0   21   Bill
1   28  Steve
2   22   John
3   30   John
4   29   John

In [76]: df.sort_values('age').drop_duplicates('name', keep='last')
Out[76]:
   age   name
0   21   Bill
1   28  Steve
3   30   John
</code></pre>

<p>or this depending on your goals:</p>

<pre><code>In [77]: df.drop_duplicates('name', keep='last')
Out[77]:
   age   name
0   21   Bill
1   28  Steve
4   29   John
</code></pre>

python pandas dataframe