cujo30227
cujo30227

Reputation: 770

Pandas dataframe get first row of each group and copy to other rows

I have a pandas DataFrame like following.

df = pd.DataFrame({'id' : [1,1,1,2,2,3,3,3,3,4,4,5,6,6,6,7,7],
                'value'  : ["first","second","second","first",
                            "second","first","third","fourth",
                            "fifth","second","fifth","first",
                            "first","second","third","fourth","fifth"]})

I want to group this by ["id","value"], get the first row of each group, and use its value to overwrite the values in the remaining rows of the group (so the dimensions of the resulting table is the same as the source table)

        id   value
0        1   first
1        1  second
2        1  second
3        2  second
4        2   first
5        3   first
6        3   third
7        3  fourth
8        3   fifth
9        4  second
10       4   fifth
11       5   first
12       6   first
13       6  second
14       6   third
15       7  fourth
16       7   fifth

Expected outcome

        id   value
0        1   first
1        1   first
2        1   first
3        2  second
4        2  second
5        3   first
6        3   first
7        3   first
8        3   first
9        4  second
10       4  second
11       5   first
12       6   first
13       6   first
14       6   first
15       7  fourth
16       7  fourth

I tried numerous approaches, but to no avail... Any ideas?

Upvotes: 1

Views: 1087

Answers (1)

jezrael
jezrael

Reputation: 863166

Use GroupBy.transform with GroupBy.first:

df['value'] = df.groupby('id')['value'].transform('first')

Upvotes: 4

Related Questions