column not getting attached to dataframe
up vote
0
down vote
favorite
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.
python-3.x pandas
add a comment |
up vote
0
down vote
favorite
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.
python-3.x pandas
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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.
python-3.x pandas
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.
python-3.x pandas
python-3.x pandas
edited Nov 12 at 5:15
asked Nov 12 at 5:13
Rahul rajan
1279
1279
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
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
add a comment |
up vote
1
down vote
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)))
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256267%2fcolumn-not-getting-attached-to-dataframe%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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
add a comment |
up vote
1
down vote
accepted
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
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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
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
edited Nov 12 at 5:36
answered Nov 12 at 5:19
Sandeep Kadapa
5,642427
5,642427
add a comment |
add a comment |
up vote
1
down vote
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)))
add a comment |
up vote
1
down vote
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)))
add a comment |
up vote
1
down vote
up vote
1
down vote
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)))
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)))
answered Nov 12 at 5:15
Mayank Porwal
4,1011621
4,1011621
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256267%2fcolumn-not-getting-attached-to-dataframe%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown