Q: column not getting attached to dataframe I am using the below code to do groupby calaculation.
**Input**
ORG DSTN    FLAG
LON SIN      Y
ADL SIN      N
SIN LON      N
LON SIN      Y
LON SIN      N
ADL SIN      Y
ADL SIN      N
SIN LON      Y
SIN LON      Y
SIN LON      Y
SIN LON      N
LON SIN      N

My Code
data.assign(Lane_Score=data.groupby(['ORIGIN_CITY','DEST_CITY']).Delay_Flag.apply(lambda x:x.replace('YES|NO',(x=='YES').mean(),regex=True)))

I am getting  the output by its not getting attached to dataframe.When I try to extract that column alone its not working. 
data['Lane_Score']

How to fix this.

A: Try this:
data['Lane_Score'] = data.groupby(['ORIGIN_CITY','DEST_CITY']).Delay_Flag.apply(lambda x:x.replace('YES|NO',(x=='YES').mean(),regex=True)))


A: According to pandas documentation DataFrame.assign

keywords are the column names. If the values are callable, they are computed on the DataFrame and assigned to the new columns. The callable must not change input DataFrame (though pandas doesn't check it). If the values are not callable, (e.g. a Series, scalar, or array), they are simply assigned.

So you either need to assign it to original df or use explicit general assignment, Also you don't need replace use transform:
df['Lane_Score'] = df.groupby(['ORG','DSTN']).FLAG.transform(lambda x: (x=='Y').mean())

Or Faster approach would be:
df['Lane_Score'] = df['FLAG']=='Y'
df['Lane_Score'] = df.groupby(['ORG','DSTN']).Lane_Score.transform('mean')

Or:
df = df.assign(Lane_Score=df.groupby(['ORG','DSTN']).FLAG.apply(lambda x: x.replace('Y|N',(x=='Y').mean(),regex=True)))


print(df)
    ORG DSTN FLAG  Lane_Score
0   LON  SIN    Y    0.500000
1   ADL  SIN    N    0.333333
2   SIN  LON    N    0.600000
3   LON  SIN    Y    0.500000
4   LON  SIN    N    0.500000
5   ADL  SIN    Y    0.333333
6   ADL  SIN    N    0.333333
7   SIN  LON    Y    0.600000
8   SIN  LON    Y    0.600000
9   SIN  LON    Y    0.600000
10  SIN  LON    N    0.600000
11  LON  SIN    N    0.500000

