Replace original rows with duplicate rows then remove duplicates

Viewed 55

I have a dataframe similar to

    Name   Value  Value2
     A       1      11        
     B       2      12
     C       3      13
     A       4      14
     B       5      15

I need to take the rows were Name is a duplicate and replace the rows with the first occurrence with the last occurrence and then remove the last duplicates. So in this example the output would be

    Name   Value  Value2
     A       4      14
     B       5      15
     C       3      13    

What would be a efficient way of doing this?

4 Answers

Like this?

data.drop_duplicates(subset='Name', keep='last').sort_values('Name')

Try to use

df = df.groupby('Name',as_index=False).nth(-1)

Use groupby.last:

df.groupby('Name',as_index=False).last()

Output:

  Name  Value  Value2
0    A      4      14
1    B      5      15
2    C      3      13

Since you want to keep the last occurence of each individual Name value, you wouldn't care of replacing the earlier ones. You should just drop duplicates, filtered by Name and pass the parameter keep='last':

data = {'Name':['A','B','C','A','B'],'Value':[1,2,3,4,5],'Value2':[11,12,13,14,15]}
df = pd.DataFrame(data)
df = df.drop_duplicates('Name',keep='last')
print(DataFrame)

Output:

  Name  Value  Value2
2    C      3      13
3    A      4      14
4    B      5      15
Related