0

I have a dataframe that looks like :

GroupID | Name
 1      | aaa
 2      | 
 2      | bbb
 1      | 
 1      | 

The idea would be to expand all Name values to every line with the same GroupID :

GroupID | Name
 1      | aaa
 2      | bbb
 2      | bbb
 1      | aaa
 1      | aaa

How would I do that easily ?

1
  • 1
    df.groupby("GroupID")["Name"].bfill().ffill() Commented Jun 15, 2021 at 9:08

1 Answer 1

1

You could use a dictionary:

dct = dict(df[~df['Name'].isna()].values)
df['Name'] = df['GroupID'].replace(dct)

   GroupID Name
0        1  aaa
1        2  bbb
2        2  bbb
3        1  aaa
4        1  aaa
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.