Checking for only column value to be present in dataframe












1














I have a following below dataframe :



          col1                col2         col3
0 1601286 NAN NAN
1 1601286 1135 2018-12-31
2 1601286 NAN NAN
3 1601286 1135 2018-12-31
4 1601286 NAN 2018-12-31
5 1601286 1135 2018-12-31
6 1601286 1135 2018-12-31
7 1601286 1135 2018-12-31
8 1601286 NAN NAN


I need to put a validation that only one out of these 3 columns should have a value . If more than one is notnull(), it should be a False.



For example, the output of above data should be ,



0 True
1 False
2 True
3 False
4 False
5 False
6 False
7 False
8 True


trying to do something like below which is for sure will not work:-



df= df[['col1', 'col2', 'col3']].notnull().any(axis=1)


How can I handle this.










share|improve this question
























  • Should the validation be True or False when there are more than one NaN per row?
    – jorijnsmit
    2 days ago
















1














I have a following below dataframe :



          col1                col2         col3
0 1601286 NAN NAN
1 1601286 1135 2018-12-31
2 1601286 NAN NAN
3 1601286 1135 2018-12-31
4 1601286 NAN 2018-12-31
5 1601286 1135 2018-12-31
6 1601286 1135 2018-12-31
7 1601286 1135 2018-12-31
8 1601286 NAN NAN


I need to put a validation that only one out of these 3 columns should have a value . If more than one is notnull(), it should be a False.



For example, the output of above data should be ,



0 True
1 False
2 True
3 False
4 False
5 False
6 False
7 False
8 True


trying to do something like below which is for sure will not work:-



df= df[['col1', 'col2', 'col3']].notnull().any(axis=1)


How can I handle this.










share|improve this question
























  • Should the validation be True or False when there are more than one NaN per row?
    – jorijnsmit
    2 days ago














1












1








1







I have a following below dataframe :



          col1                col2         col3
0 1601286 NAN NAN
1 1601286 1135 2018-12-31
2 1601286 NAN NAN
3 1601286 1135 2018-12-31
4 1601286 NAN 2018-12-31
5 1601286 1135 2018-12-31
6 1601286 1135 2018-12-31
7 1601286 1135 2018-12-31
8 1601286 NAN NAN


I need to put a validation that only one out of these 3 columns should have a value . If more than one is notnull(), it should be a False.



For example, the output of above data should be ,



0 True
1 False
2 True
3 False
4 False
5 False
6 False
7 False
8 True


trying to do something like below which is for sure will not work:-



df= df[['col1', 'col2', 'col3']].notnull().any(axis=1)


How can I handle this.










share|improve this question















I have a following below dataframe :



          col1                col2         col3
0 1601286 NAN NAN
1 1601286 1135 2018-12-31
2 1601286 NAN NAN
3 1601286 1135 2018-12-31
4 1601286 NAN 2018-12-31
5 1601286 1135 2018-12-31
6 1601286 1135 2018-12-31
7 1601286 1135 2018-12-31
8 1601286 NAN NAN


I need to put a validation that only one out of these 3 columns should have a value . If more than one is notnull(), it should be a False.



For example, the output of above data should be ,



0 True
1 False
2 True
3 False
4 False
5 False
6 False
7 False
8 True


trying to do something like below which is for sure will not work:-



df= df[['col1', 'col2', 'col3']].notnull().any(axis=1)


How can I handle this.







python pandas dataframe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago

























asked 2 days ago









user1896796

129217




129217












  • Should the validation be True or False when there are more than one NaN per row?
    – jorijnsmit
    2 days ago


















  • Should the validation be True or False when there are more than one NaN per row?
    – jorijnsmit
    2 days ago
















Should the validation be True or False when there are more than one NaN per row?
– jorijnsmit
2 days ago




Should the validation be True or False when there are more than one NaN per row?
– jorijnsmit
2 days ago












3 Answers
3






active

oldest

votes


















4














Using isnull and sum:



df.isnull().sum(1).eq(2)


or:



df.isnull().sum(1).gt(1)


or:



df.notnull().sum(1).lt(2)


or:



df.notnull().sum(1).eq(1)

0 True
1 False
2 True
3 False
4 False
5 False
6 False
7 False
8 True
dtype: bool





share|improve this answer



















  • 1




    Perfect solution :)
    – cph_sto
    2 days ago










  • @Sandeep I thought I would be able to solve my issue with this . Can you help me in this question please. stackoverflow.com/questions/53946579/…
    – user1896796
    2 days ago



















1














pandas.isnull together with pandas.sum and then check your condition. For example



import pandas as pd
import numpy as np

d = {'A':[1, 2, 3, np.NaN, 5], 'B':[1, 2, np.NaN, np.NaN, 5], 'C':[1, 2, np.NaN, np.NaN, np.NaN]}
print(pd.DataFrame(d).isnull().sum(axis=1)>1)


Output



0    False
1 False
2 True
3 True
4 False
dtype: bool





share|improve this answer





















  • Can you help me in this question please. stackoverflow.com/questions/53946579/…
    – user1896796
    2 days ago



















1














df = pd.DataFrame([[1601286,np.NaN,np.NaN],
[1601286,1135,2018-12-31],
[1601286,np.NaN,np.NaN],
[1601286,1135,2018-12-31],
[1601286,np.NaN,2018-12-31],
[1601286,1135,2018-12-31],
[1601286,1135,2018-12-31],
[1601286,1135,2018-12-31],
[1601286,np.NaN,np.NaN]], columns=['col1','col2','col3'])

df['count_notnull']=df.count(axis=1) # Will give a count of non-NULLs.
df['bool'] = df['count_notnull'].map(lambda x:x==1) # Since we need only 1 non-Null,
# so we test the condition here.

df

col1 col2 col3 count_notnull bool
0 1601286 NaN NaN 1 True
1 1601286 1135.0 1975.0 3 False
2 1601286 NaN NaN 1 True
3 1601286 1135.0 1975.0 3 False
4 1601286 NaN 1975.0 2 False
5 1601286 1135.0 1975.0 3 False
6 1601286 1135.0 1975.0 3 False
7 1601286 1135.0 1975.0 3 False
8 1601286 NaN NaN 1 True





share|improve this answer























  • Can you help me in this question please. stackoverflow.com/questions/53946579/…
    – user1896796
    2 days ago










  • I can try. Give me some moments.
    – cph_sto
    2 days ago











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%2f53945345%2fchecking-for-only-column-value-to-be-present-in-dataframe%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














Using isnull and sum:



df.isnull().sum(1).eq(2)


or:



df.isnull().sum(1).gt(1)


or:



df.notnull().sum(1).lt(2)


or:



df.notnull().sum(1).eq(1)

0 True
1 False
2 True
3 False
4 False
5 False
6 False
7 False
8 True
dtype: bool





share|improve this answer



















  • 1




    Perfect solution :)
    – cph_sto
    2 days ago










  • @Sandeep I thought I would be able to solve my issue with this . Can you help me in this question please. stackoverflow.com/questions/53946579/…
    – user1896796
    2 days ago
















4














Using isnull and sum:



df.isnull().sum(1).eq(2)


or:



df.isnull().sum(1).gt(1)


or:



df.notnull().sum(1).lt(2)


or:



df.notnull().sum(1).eq(1)

0 True
1 False
2 True
3 False
4 False
5 False
6 False
7 False
8 True
dtype: bool





share|improve this answer



















  • 1




    Perfect solution :)
    – cph_sto
    2 days ago










  • @Sandeep I thought I would be able to solve my issue with this . Can you help me in this question please. stackoverflow.com/questions/53946579/…
    – user1896796
    2 days ago














4












4








4






Using isnull and sum:



df.isnull().sum(1).eq(2)


or:



df.isnull().sum(1).gt(1)


or:



df.notnull().sum(1).lt(2)


or:



df.notnull().sum(1).eq(1)

0 True
1 False
2 True
3 False
4 False
5 False
6 False
7 False
8 True
dtype: bool





share|improve this answer














Using isnull and sum:



df.isnull().sum(1).eq(2)


or:



df.isnull().sum(1).gt(1)


or:



df.notnull().sum(1).lt(2)


or:



df.notnull().sum(1).eq(1)

0 True
1 False
2 True
3 False
4 False
5 False
6 False
7 False
8 True
dtype: bool






share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered 2 days ago









Sandeep Kadapa

5,927428




5,927428








  • 1




    Perfect solution :)
    – cph_sto
    2 days ago










  • @Sandeep I thought I would be able to solve my issue with this . Can you help me in this question please. stackoverflow.com/questions/53946579/…
    – user1896796
    2 days ago














  • 1




    Perfect solution :)
    – cph_sto
    2 days ago










  • @Sandeep I thought I would be able to solve my issue with this . Can you help me in this question please. stackoverflow.com/questions/53946579/…
    – user1896796
    2 days ago








1




1




Perfect solution :)
– cph_sto
2 days ago




Perfect solution :)
– cph_sto
2 days ago












@Sandeep I thought I would be able to solve my issue with this . Can you help me in this question please. stackoverflow.com/questions/53946579/…
– user1896796
2 days ago




@Sandeep I thought I would be able to solve my issue with this . Can you help me in this question please. stackoverflow.com/questions/53946579/…
– user1896796
2 days ago













1














pandas.isnull together with pandas.sum and then check your condition. For example



import pandas as pd
import numpy as np

d = {'A':[1, 2, 3, np.NaN, 5], 'B':[1, 2, np.NaN, np.NaN, 5], 'C':[1, 2, np.NaN, np.NaN, np.NaN]}
print(pd.DataFrame(d).isnull().sum(axis=1)>1)


Output



0    False
1 False
2 True
3 True
4 False
dtype: bool





share|improve this answer





















  • Can you help me in this question please. stackoverflow.com/questions/53946579/…
    – user1896796
    2 days ago
















1














pandas.isnull together with pandas.sum and then check your condition. For example



import pandas as pd
import numpy as np

d = {'A':[1, 2, 3, np.NaN, 5], 'B':[1, 2, np.NaN, np.NaN, 5], 'C':[1, 2, np.NaN, np.NaN, np.NaN]}
print(pd.DataFrame(d).isnull().sum(axis=1)>1)


Output



0    False
1 False
2 True
3 True
4 False
dtype: bool





share|improve this answer





















  • Can you help me in this question please. stackoverflow.com/questions/53946579/…
    – user1896796
    2 days ago














1












1








1






pandas.isnull together with pandas.sum and then check your condition. For example



import pandas as pd
import numpy as np

d = {'A':[1, 2, 3, np.NaN, 5], 'B':[1, 2, np.NaN, np.NaN, 5], 'C':[1, 2, np.NaN, np.NaN, np.NaN]}
print(pd.DataFrame(d).isnull().sum(axis=1)>1)


Output



0    False
1 False
2 True
3 True
4 False
dtype: bool





share|improve this answer












pandas.isnull together with pandas.sum and then check your condition. For example



import pandas as pd
import numpy as np

d = {'A':[1, 2, 3, np.NaN, 5], 'B':[1, 2, np.NaN, np.NaN, 5], 'C':[1, 2, np.NaN, np.NaN, np.NaN]}
print(pd.DataFrame(d).isnull().sum(axis=1)>1)


Output



0    False
1 False
2 True
3 True
4 False
dtype: bool






share|improve this answer












share|improve this answer



share|improve this answer










answered 2 days ago









b-fg

1,95411422




1,95411422












  • Can you help me in this question please. stackoverflow.com/questions/53946579/…
    – user1896796
    2 days ago


















  • Can you help me in this question please. stackoverflow.com/questions/53946579/…
    – user1896796
    2 days ago
















Can you help me in this question please. stackoverflow.com/questions/53946579/…
– user1896796
2 days ago




Can you help me in this question please. stackoverflow.com/questions/53946579/…
– user1896796
2 days ago











1














df = pd.DataFrame([[1601286,np.NaN,np.NaN],
[1601286,1135,2018-12-31],
[1601286,np.NaN,np.NaN],
[1601286,1135,2018-12-31],
[1601286,np.NaN,2018-12-31],
[1601286,1135,2018-12-31],
[1601286,1135,2018-12-31],
[1601286,1135,2018-12-31],
[1601286,np.NaN,np.NaN]], columns=['col1','col2','col3'])

df['count_notnull']=df.count(axis=1) # Will give a count of non-NULLs.
df['bool'] = df['count_notnull'].map(lambda x:x==1) # Since we need only 1 non-Null,
# so we test the condition here.

df

col1 col2 col3 count_notnull bool
0 1601286 NaN NaN 1 True
1 1601286 1135.0 1975.0 3 False
2 1601286 NaN NaN 1 True
3 1601286 1135.0 1975.0 3 False
4 1601286 NaN 1975.0 2 False
5 1601286 1135.0 1975.0 3 False
6 1601286 1135.0 1975.0 3 False
7 1601286 1135.0 1975.0 3 False
8 1601286 NaN NaN 1 True





share|improve this answer























  • Can you help me in this question please. stackoverflow.com/questions/53946579/…
    – user1896796
    2 days ago










  • I can try. Give me some moments.
    – cph_sto
    2 days ago
















1














df = pd.DataFrame([[1601286,np.NaN,np.NaN],
[1601286,1135,2018-12-31],
[1601286,np.NaN,np.NaN],
[1601286,1135,2018-12-31],
[1601286,np.NaN,2018-12-31],
[1601286,1135,2018-12-31],
[1601286,1135,2018-12-31],
[1601286,1135,2018-12-31],
[1601286,np.NaN,np.NaN]], columns=['col1','col2','col3'])

df['count_notnull']=df.count(axis=1) # Will give a count of non-NULLs.
df['bool'] = df['count_notnull'].map(lambda x:x==1) # Since we need only 1 non-Null,
# so we test the condition here.

df

col1 col2 col3 count_notnull bool
0 1601286 NaN NaN 1 True
1 1601286 1135.0 1975.0 3 False
2 1601286 NaN NaN 1 True
3 1601286 1135.0 1975.0 3 False
4 1601286 NaN 1975.0 2 False
5 1601286 1135.0 1975.0 3 False
6 1601286 1135.0 1975.0 3 False
7 1601286 1135.0 1975.0 3 False
8 1601286 NaN NaN 1 True





share|improve this answer























  • Can you help me in this question please. stackoverflow.com/questions/53946579/…
    – user1896796
    2 days ago










  • I can try. Give me some moments.
    – cph_sto
    2 days ago














1












1








1






df = pd.DataFrame([[1601286,np.NaN,np.NaN],
[1601286,1135,2018-12-31],
[1601286,np.NaN,np.NaN],
[1601286,1135,2018-12-31],
[1601286,np.NaN,2018-12-31],
[1601286,1135,2018-12-31],
[1601286,1135,2018-12-31],
[1601286,1135,2018-12-31],
[1601286,np.NaN,np.NaN]], columns=['col1','col2','col3'])

df['count_notnull']=df.count(axis=1) # Will give a count of non-NULLs.
df['bool'] = df['count_notnull'].map(lambda x:x==1) # Since we need only 1 non-Null,
# so we test the condition here.

df

col1 col2 col3 count_notnull bool
0 1601286 NaN NaN 1 True
1 1601286 1135.0 1975.0 3 False
2 1601286 NaN NaN 1 True
3 1601286 1135.0 1975.0 3 False
4 1601286 NaN 1975.0 2 False
5 1601286 1135.0 1975.0 3 False
6 1601286 1135.0 1975.0 3 False
7 1601286 1135.0 1975.0 3 False
8 1601286 NaN NaN 1 True





share|improve this answer














df = pd.DataFrame([[1601286,np.NaN,np.NaN],
[1601286,1135,2018-12-31],
[1601286,np.NaN,np.NaN],
[1601286,1135,2018-12-31],
[1601286,np.NaN,2018-12-31],
[1601286,1135,2018-12-31],
[1601286,1135,2018-12-31],
[1601286,1135,2018-12-31],
[1601286,np.NaN,np.NaN]], columns=['col1','col2','col3'])

df['count_notnull']=df.count(axis=1) # Will give a count of non-NULLs.
df['bool'] = df['count_notnull'].map(lambda x:x==1) # Since we need only 1 non-Null,
# so we test the condition here.

df

col1 col2 col3 count_notnull bool
0 1601286 NaN NaN 1 True
1 1601286 1135.0 1975.0 3 False
2 1601286 NaN NaN 1 True
3 1601286 1135.0 1975.0 3 False
4 1601286 NaN 1975.0 2 False
5 1601286 1135.0 1975.0 3 False
6 1601286 1135.0 1975.0 3 False
7 1601286 1135.0 1975.0 3 False
8 1601286 NaN NaN 1 True






share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered 2 days ago









cph_sto

1,203219




1,203219












  • Can you help me in this question please. stackoverflow.com/questions/53946579/…
    – user1896796
    2 days ago










  • I can try. Give me some moments.
    – cph_sto
    2 days ago


















  • Can you help me in this question please. stackoverflow.com/questions/53946579/…
    – user1896796
    2 days ago










  • I can try. Give me some moments.
    – cph_sto
    2 days ago
















Can you help me in this question please. stackoverflow.com/questions/53946579/…
– user1896796
2 days ago




Can you help me in this question please. stackoverflow.com/questions/53946579/…
– user1896796
2 days ago












I can try. Give me some moments.
– cph_sto
2 days ago




I can try. Give me some moments.
– cph_sto
2 days ago


















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.





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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53945345%2fchecking-for-only-column-value-to-be-present-in-dataframe%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







Popular posts from this blog

generate and download xml file after input submit (php and mysql) - JPK

Angular Downloading a file using contenturl with Basic Authentication

Can't read property showImagePicker of undefined in react native iOS