5

What I'm trying to achieve is that when a row in col2 has a 1, it will copy that 1 onto all the other values in col2 as long as the rows in col1 have the same name. As an example, if the dataframe looks like this

col1  col2
xx      1
xx      0
xx      0
xx      0
yy      0
yy      0
yy      0
zz      0
zz      0
zz      1

The output would be

col1  col2
xx      1
xx      1
xx      1
xx      1
yy      0
yy      0
yy      0
zz      1
zz      1
zz      1
Giuseppe La Gualano
  • 1,491
  • 1
  • 4
  • 24
Hatarax
  • 53
  • 3

2 Answers2

6

Use groupby.transform('max'):

df['col2'] = df.groupby('col1')['col2'].transform('max')

Output:

  col1  col2
0   xx     1
1   xx     1
2   xx     1
3   xx     1
4   yy     0
5   yy     0
6   yy     0
7   zz     1
8   zz     1
9   zz     1
mozway
  • 194,879
  • 13
  • 39
  • 75
0

The generic trick here is to perform a .groupby that checks is any value is equal to 1. Then map your output back over that boolean return.

df['col2'] = (
    df['col2'].eq(1)
    .groupby(df['col1']).transform('any')
    .map({True: 1, False: 0}) # could also use `.astype(int)`
)

print(df)
  col1  col2
0   xx     1
1   xx     1
2   xx     1
3   xx     1
4   yy     0
5   yy     0
6   yy     0
7   zz     1
8   zz     1
9   zz     1
Cameron Riddell
  • 10,942
  • 9
  • 19