0

I have a df like this

    Name    Data   
0   Mike    123    
1   Mike    456    
2   Mike    789    
3   Fred    345
4   Fred    123
5   Ted     333

I need to get unique Name with the max index value

output:

    Name    Data   
0   Mike    789    
1   Fred    123
2   Ted     333
jason
  • 3,811
  • 18
  • 92
  • 147

1 Answers1

2

Step 1st: Import pandas.

import pandas as pd

Step 2nd: Copy OP's df values.

Step 3rd: Now run following command to create data frame from OP's samples.

df=pd.read_clipboard()

Step 4th: Run following code to remove duplicates and keep last value of Name column.

df.drop_duplicates(subset='Name',keep='last')

Output will be as follows.

   Name   Data
2   Mike   789 
4   Fred   123 
5   Ted    333 
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • 1
    wow. Amazingly simple. Thanks. This answer is so much better than the groupby solutions. So easy to understand and readable. ... Also.. +1 for the `read_clipboard()`... didn't know you could do that... that's also amazing! – jason Dec 10 '18 at 03:15