1

I have a pandas dataframe has two columns, code and name,

each duplicated code may have different names,

How can I replace name for each code with last occurrence for each one

code name
1 3
1 6
2 5
3 4
1 7

Required output

code name
1 7
1 7
2 5
3 4
1 7

1 Answer 1

2

You can use pd.groupby() to group by the column code and get the last value from the column name for each value of code. Use transform to get the complete column back and save it under the column name:

df['name'] = df.groupby('code').name.transform('last')
print(df)

Output:

   code  name
0     1     7
1     1     7
2     2     5
3     3     4
4     1     7
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.