I need to transform pandas toy-dataframe like this (basically group by entity, find the value of v for which df['gr'] == 'x' and "expand" that value to the entire grouping):
entity gr v
0 A x 1
1 A y 2
2 A z 3
3 B z 4
4 B x 5
5 B y 6
to this form:
entity gr v new
0 A x 1 1
1 A y 2 1
2 A z 3 1
3 B z 4 5
4 B x 5 5
5 B y 6 5
Here is my solution:
import pandas as pd
df = pd.DataFrame({'entity': ['A', 'A', 'A','B', 'B', 'B'], 'gr': ['x', 'y', 'z', 'z', 'x', 'y'], 'v': [1,2,3,4,5,6]})
df['new'] = df.loc[df['gr'] == 'x', 'v']
df['new'] = df.groupby('entity')['new'].ffill().bfill().astype(int)
but I wonder, if a better, more concise or idiomatic approach exists to this problem?
Slight variation on this problem, instead of df['gr'] == 'x', different mask df['gr'] == df['different_column']