if nan python pandas loop












0















This is a bit weird and I don't have a sample data frame but if anyone can help that would be great.



I have 3 columns A, B and C.



C might be blank / nan.



I was to say, if C is blank and A and B equal the same values as A and B of the row above. Then set C to the same value as C of the row above.



this is what I have so far. Its running but not changing the values of C.



for i, row in df.iterrows():

if df['C'][i]==np.nan:
if df[['A','B']][i]==df[['A','B']][i-1]:
df['C'][i]=df['C'][i-1]
else:
pass
else:
pass


Does anyone see why this might not be working?



Many thanks



I've also tried this but this code is not working at all



 for i, row in df.iterrows():

if df['C'][i]==np.nan & df[['A','B']][i]==df[['A','B']][i-1]:
df['C'][i]=df['C'][i-1]

else:
pass


so df:



A    B    C
w 4 t
w 4
a r c


Output should be :



  A    B    C
w 4 t
w 4 t
a r c









share|improve this question

























  • Got the answer thanks to the guys below. If anyone see what's wrong with the loop please do comment, as I'm really curious now

    – fred.schwartz
    Nov 22 '18 at 10:25
















0















This is a bit weird and I don't have a sample data frame but if anyone can help that would be great.



I have 3 columns A, B and C.



C might be blank / nan.



I was to say, if C is blank and A and B equal the same values as A and B of the row above. Then set C to the same value as C of the row above.



this is what I have so far. Its running but not changing the values of C.



for i, row in df.iterrows():

if df['C'][i]==np.nan:
if df[['A','B']][i]==df[['A','B']][i-1]:
df['C'][i]=df['C'][i-1]
else:
pass
else:
pass


Does anyone see why this might not be working?



Many thanks



I've also tried this but this code is not working at all



 for i, row in df.iterrows():

if df['C'][i]==np.nan & df[['A','B']][i]==df[['A','B']][i-1]:
df['C'][i]=df['C'][i-1]

else:
pass


so df:



A    B    C
w 4 t
w 4
a r c


Output should be :



  A    B    C
w 4 t
w 4 t
a r c









share|improve this question

























  • Got the answer thanks to the guys below. If anyone see what's wrong with the loop please do comment, as I'm really curious now

    – fred.schwartz
    Nov 22 '18 at 10:25














0












0








0








This is a bit weird and I don't have a sample data frame but if anyone can help that would be great.



I have 3 columns A, B and C.



C might be blank / nan.



I was to say, if C is blank and A and B equal the same values as A and B of the row above. Then set C to the same value as C of the row above.



this is what I have so far. Its running but not changing the values of C.



for i, row in df.iterrows():

if df['C'][i]==np.nan:
if df[['A','B']][i]==df[['A','B']][i-1]:
df['C'][i]=df['C'][i-1]
else:
pass
else:
pass


Does anyone see why this might not be working?



Many thanks



I've also tried this but this code is not working at all



 for i, row in df.iterrows():

if df['C'][i]==np.nan & df[['A','B']][i]==df[['A','B']][i-1]:
df['C'][i]=df['C'][i-1]

else:
pass


so df:



A    B    C
w 4 t
w 4
a r c


Output should be :



  A    B    C
w 4 t
w 4 t
a r c









share|improve this question
















This is a bit weird and I don't have a sample data frame but if anyone can help that would be great.



I have 3 columns A, B and C.



C might be blank / nan.



I was to say, if C is blank and A and B equal the same values as A and B of the row above. Then set C to the same value as C of the row above.



this is what I have so far. Its running but not changing the values of C.



for i, row in df.iterrows():

if df['C'][i]==np.nan:
if df[['A','B']][i]==df[['A','B']][i-1]:
df['C'][i]=df['C'][i-1]
else:
pass
else:
pass


Does anyone see why this might not be working?



Many thanks



I've also tried this but this code is not working at all



 for i, row in df.iterrows():

if df['C'][i]==np.nan & df[['A','B']][i]==df[['A','B']][i-1]:
df['C'][i]=df['C'][i-1]

else:
pass


so df:



A    B    C
w 4 t
w 4
a r c


Output should be :



  A    B    C
w 4 t
w 4 t
a r c






pandas loops if-statement






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 10:13







fred.schwartz

















asked Nov 22 '18 at 10:06









fred.schwartzfred.schwartz

42210




42210













  • Got the answer thanks to the guys below. If anyone see what's wrong with the loop please do comment, as I'm really curious now

    – fred.schwartz
    Nov 22 '18 at 10:25



















  • Got the answer thanks to the guys below. If anyone see what's wrong with the loop please do comment, as I'm really curious now

    – fred.schwartz
    Nov 22 '18 at 10:25

















Got the answer thanks to the guys below. If anyone see what's wrong with the loop please do comment, as I'm really curious now

– fred.schwartz
Nov 22 '18 at 10:25





Got the answer thanks to the guys below. If anyone see what's wrong with the loop please do comment, as I'm really curious now

– fred.schwartz
Nov 22 '18 at 10:25












2 Answers
2






active

oldest

votes


















1














You should try np.where and DataFrame.shift:



df = pd.DataFrame({'A':np.random.randint(0, 20, size = 100),
'B': np.random.randint(0, 20, size = 100),
'C':np.random.randint(0, 20, size = 100)})

A B C
0 9 0 16
1 15 15 13
2 9 1 4
3 14 13 18
4 4 14 10



df['C'] = np.where((df['A'] == df['A'].shift(1)) & (df['B'] == df['B'].shift(1))& (df['C'] == np.nan), df['C_shift'], df['C'])

np.sum(df['C'] == df['C'].shift(
>>3





share|improve this answer


























  • Worked perfectly. Thanks a lot

    – fred.schwartz
    Nov 22 '18 at 10:24











  • @fred.schwartz Happy to help :}

    – Mohit Motwani
    Nov 22 '18 at 10:25



















1














You can use:



df['C'] = np.where((df['A']==df['A'].shift()) & (df['B']==df['B'].shift()) & (df['C'].isnull()), df['C'].shift(), df['C'] )





share|improve this answer
























  • This worked. Thanks a lot

    – fred.schwartz
    Nov 22 '18 at 10:25











  • @fred.schwartz you are welcome!

    – Joe
    Nov 22 '18 at 10:26











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',
autoActivateHeartbeat: false,
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428446%2fif-nan-python-pandas-loop%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









1














You should try np.where and DataFrame.shift:



df = pd.DataFrame({'A':np.random.randint(0, 20, size = 100),
'B': np.random.randint(0, 20, size = 100),
'C':np.random.randint(0, 20, size = 100)})

A B C
0 9 0 16
1 15 15 13
2 9 1 4
3 14 13 18
4 4 14 10



df['C'] = np.where((df['A'] == df['A'].shift(1)) & (df['B'] == df['B'].shift(1))& (df['C'] == np.nan), df['C_shift'], df['C'])

np.sum(df['C'] == df['C'].shift(
>>3





share|improve this answer


























  • Worked perfectly. Thanks a lot

    – fred.schwartz
    Nov 22 '18 at 10:24











  • @fred.schwartz Happy to help :}

    – Mohit Motwani
    Nov 22 '18 at 10:25
















1














You should try np.where and DataFrame.shift:



df = pd.DataFrame({'A':np.random.randint(0, 20, size = 100),
'B': np.random.randint(0, 20, size = 100),
'C':np.random.randint(0, 20, size = 100)})

A B C
0 9 0 16
1 15 15 13
2 9 1 4
3 14 13 18
4 4 14 10



df['C'] = np.where((df['A'] == df['A'].shift(1)) & (df['B'] == df['B'].shift(1))& (df['C'] == np.nan), df['C_shift'], df['C'])

np.sum(df['C'] == df['C'].shift(
>>3





share|improve this answer


























  • Worked perfectly. Thanks a lot

    – fred.schwartz
    Nov 22 '18 at 10:24











  • @fred.schwartz Happy to help :}

    – Mohit Motwani
    Nov 22 '18 at 10:25














1












1








1







You should try np.where and DataFrame.shift:



df = pd.DataFrame({'A':np.random.randint(0, 20, size = 100),
'B': np.random.randint(0, 20, size = 100),
'C':np.random.randint(0, 20, size = 100)})

A B C
0 9 0 16
1 15 15 13
2 9 1 4
3 14 13 18
4 4 14 10



df['C'] = np.where((df['A'] == df['A'].shift(1)) & (df['B'] == df['B'].shift(1))& (df['C'] == np.nan), df['C_shift'], df['C'])

np.sum(df['C'] == df['C'].shift(
>>3





share|improve this answer















You should try np.where and DataFrame.shift:



df = pd.DataFrame({'A':np.random.randint(0, 20, size = 100),
'B': np.random.randint(0, 20, size = 100),
'C':np.random.randint(0, 20, size = 100)})

A B C
0 9 0 16
1 15 15 13
2 9 1 4
3 14 13 18
4 4 14 10



df['C'] = np.where((df['A'] == df['A'].shift(1)) & (df['B'] == df['B'].shift(1))& (df['C'] == np.nan), df['C_shift'], df['C'])

np.sum(df['C'] == df['C'].shift(
>>3






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 10:26

























answered Nov 22 '18 at 10:21









Mohit MotwaniMohit Motwani

2,2151725




2,2151725













  • Worked perfectly. Thanks a lot

    – fred.schwartz
    Nov 22 '18 at 10:24











  • @fred.schwartz Happy to help :}

    – Mohit Motwani
    Nov 22 '18 at 10:25



















  • Worked perfectly. Thanks a lot

    – fred.schwartz
    Nov 22 '18 at 10:24











  • @fred.schwartz Happy to help :}

    – Mohit Motwani
    Nov 22 '18 at 10:25

















Worked perfectly. Thanks a lot

– fred.schwartz
Nov 22 '18 at 10:24





Worked perfectly. Thanks a lot

– fred.schwartz
Nov 22 '18 at 10:24













@fred.schwartz Happy to help :}

– Mohit Motwani
Nov 22 '18 at 10:25





@fred.schwartz Happy to help :}

– Mohit Motwani
Nov 22 '18 at 10:25













1














You can use:



df['C'] = np.where((df['A']==df['A'].shift()) & (df['B']==df['B'].shift()) & (df['C'].isnull()), df['C'].shift(), df['C'] )





share|improve this answer
























  • This worked. Thanks a lot

    – fred.schwartz
    Nov 22 '18 at 10:25











  • @fred.schwartz you are welcome!

    – Joe
    Nov 22 '18 at 10:26
















1














You can use:



df['C'] = np.where((df['A']==df['A'].shift()) & (df['B']==df['B'].shift()) & (df['C'].isnull()), df['C'].shift(), df['C'] )





share|improve this answer
























  • This worked. Thanks a lot

    – fred.schwartz
    Nov 22 '18 at 10:25











  • @fred.schwartz you are welcome!

    – Joe
    Nov 22 '18 at 10:26














1












1








1







You can use:



df['C'] = np.where((df['A']==df['A'].shift()) & (df['B']==df['B'].shift()) & (df['C'].isnull()), df['C'].shift(), df['C'] )





share|improve this answer













You can use:



df['C'] = np.where((df['A']==df['A'].shift()) & (df['B']==df['B'].shift()) & (df['C'].isnull()), df['C'].shift(), df['C'] )






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 10:23









JoeJoe

6,10421530




6,10421530













  • This worked. Thanks a lot

    – fred.schwartz
    Nov 22 '18 at 10:25











  • @fred.schwartz you are welcome!

    – Joe
    Nov 22 '18 at 10:26



















  • This worked. Thanks a lot

    – fred.schwartz
    Nov 22 '18 at 10:25











  • @fred.schwartz you are welcome!

    – Joe
    Nov 22 '18 at 10:26

















This worked. Thanks a lot

– fred.schwartz
Nov 22 '18 at 10:25





This worked. Thanks a lot

– fred.schwartz
Nov 22 '18 at 10:25













@fred.schwartz you are welcome!

– Joe
Nov 22 '18 at 10:26





@fred.schwartz you are welcome!

– Joe
Nov 22 '18 at 10:26


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428446%2fif-nan-python-pandas-loop%23new-answer', 'question_page');
}
);

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