Issue
I have a panda df that may have duplicate id. For each id group, for the 1st occurrence, I want wt to be filled with wt_x. The below shows the initial df and the desired df. Thank you
Initial DF
id wt wt_x
1 12 33
1 23 33
2 45 99
2 45 99
2 45 99
3 12 100
Desired DF
id wt wt_x
1 33 33
1 23 33
2 99 99
2 45 99
2 45 99
3 100 100
Solution
Try like this
import pandas as pd
data = {
'id': [1, 1, 2, 2, 2, 3],
'wt': [12, 23, 45, 45, 45, 12],
'wt_x': [33, 33, 99, 99, 99, 100]
}
df = pd.DataFrame(data)
first_occurrence_mask = ~df.duplicated(subset='id')
df.loc[first_occurrence_mask, 'wt'] = df.loc[first_occurrence_mask, 'wt_x']
print(df)
Output
id wt wt_x
0 1 33 33
1 1 23 33
2 2 99 99
3 2 45 99
4 2 45 99
5 3 100 100
Answered By - Mahboob Nur
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.