Pandas, isin, column of lists
Trying to make a Boolean flag that reads TRUE if one value or another is within a list. The below code is returning a FALSE for row 1 and I am not sure why, could someone help me understand why a FALSE is getting returned for the first row?
lists={'someList!':[[1,2,12,6,'ABC'],[1000,4,'z','a','bob']]}
dfLists = pd.DataFrame(lists)
dfLists['contains?']=dfLists['someList!'].isin([0,1])
pandas
add a comment |
Trying to make a Boolean flag that reads TRUE if one value or another is within a list. The below code is returning a FALSE for row 1 and I am not sure why, could someone help me understand why a FALSE is getting returned for the first row?
lists={'someList!':[[1,2,12,6,'ABC'],[1000,4,'z','a','bob']]}
dfLists = pd.DataFrame(lists)
dfLists['contains?']=dfLists['someList!'].isin([0,1])
pandas
Your code gives an error instead. Please provide a reproducible code.
– TeeKea
Nov 13 '18 at 16:54
add a comment |
Trying to make a Boolean flag that reads TRUE if one value or another is within a list. The below code is returning a FALSE for row 1 and I am not sure why, could someone help me understand why a FALSE is getting returned for the first row?
lists={'someList!':[[1,2,12,6,'ABC'],[1000,4,'z','a','bob']]}
dfLists = pd.DataFrame(lists)
dfLists['contains?']=dfLists['someList!'].isin([0,1])
pandas
Trying to make a Boolean flag that reads TRUE if one value or another is within a list. The below code is returning a FALSE for row 1 and I am not sure why, could someone help me understand why a FALSE is getting returned for the first row?
lists={'someList!':[[1,2,12,6,'ABC'],[1000,4,'z','a','bob']]}
dfLists = pd.DataFrame(lists)
dfLists['contains?']=dfLists['someList!'].isin([0,1])
pandas
pandas
asked Nov 13 '18 at 16:43
reality stiltsreality stilts
435
435
Your code gives an error instead. Please provide a reproducible code.
– TeeKea
Nov 13 '18 at 16:54
add a comment |
Your code gives an error instead. Please provide a reproducible code.
– TeeKea
Nov 13 '18 at 16:54
Your code gives an error instead. Please provide a reproducible code.
– TeeKea
Nov 13 '18 at 16:54
Your code gives an error instead. Please provide a reproducible code.
– TeeKea
Nov 13 '18 at 16:54
add a comment |
3 Answers
3
active
oldest
votes
Could someone help me understand why a FALSE is getting returned for the first row?
This isn't working because .isin(values) returns whether each element in the Series is contained in values.
You can use {0, 1} as a set and apply the truthiness of its intersection to each list:
>>> s = {0, 1}
>>> dfLists['someList!'].apply(lambda x: bool(s.intersection(x)))
0 True
1 False
This effectively does:
>>> s.intersection([1, 2, 12, 6, 'ABC'])
{1}
>>> s.intersection([1000, 4, 'z', 'a', 'bob'])
set()
The bool of the first result is True, because it is non-empty.
add a comment |
Using Dataframe constructor flatten you list column, then using isin
pd.DataFrame(dfLists['someList!'].tolist()).isin([1,2]).any(1)
Out[39]:
0 True
1 False
dtype: bool
thx for this answer, found it helpful
– reality stilts
Nov 13 '18 at 17:58
add a comment |
You are passing the dataframe a list of lists. It's comparing integers to lists, so it's not finding a match.
Define your columns distinctly, like this, and isin should work.
lists={'someList!':[1,2,12,6,'ABC'], 'someList2':[1000,4,'z','a','bob']}
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',
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
});
}
});
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%2f53285716%2fpandas-isin-column-of-lists%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
Could someone help me understand why a FALSE is getting returned for the first row?
This isn't working because .isin(values) returns whether each element in the Series is contained in values.
You can use {0, 1} as a set and apply the truthiness of its intersection to each list:
>>> s = {0, 1}
>>> dfLists['someList!'].apply(lambda x: bool(s.intersection(x)))
0 True
1 False
This effectively does:
>>> s.intersection([1, 2, 12, 6, 'ABC'])
{1}
>>> s.intersection([1000, 4, 'z', 'a', 'bob'])
set()
The bool of the first result is True, because it is non-empty.
add a comment |
Could someone help me understand why a FALSE is getting returned for the first row?
This isn't working because .isin(values) returns whether each element in the Series is contained in values.
You can use {0, 1} as a set and apply the truthiness of its intersection to each list:
>>> s = {0, 1}
>>> dfLists['someList!'].apply(lambda x: bool(s.intersection(x)))
0 True
1 False
This effectively does:
>>> s.intersection([1, 2, 12, 6, 'ABC'])
{1}
>>> s.intersection([1000, 4, 'z', 'a', 'bob'])
set()
The bool of the first result is True, because it is non-empty.
add a comment |
Could someone help me understand why a FALSE is getting returned for the first row?
This isn't working because .isin(values) returns whether each element in the Series is contained in values.
You can use {0, 1} as a set and apply the truthiness of its intersection to each list:
>>> s = {0, 1}
>>> dfLists['someList!'].apply(lambda x: bool(s.intersection(x)))
0 True
1 False
This effectively does:
>>> s.intersection([1, 2, 12, 6, 'ABC'])
{1}
>>> s.intersection([1000, 4, 'z', 'a', 'bob'])
set()
The bool of the first result is True, because it is non-empty.
Could someone help me understand why a FALSE is getting returned for the first row?
This isn't working because .isin(values) returns whether each element in the Series is contained in values.
You can use {0, 1} as a set and apply the truthiness of its intersection to each list:
>>> s = {0, 1}
>>> dfLists['someList!'].apply(lambda x: bool(s.intersection(x)))
0 True
1 False
This effectively does:
>>> s.intersection([1, 2, 12, 6, 'ABC'])
{1}
>>> s.intersection([1000, 4, 'z', 'a', 'bob'])
set()
The bool of the first result is True, because it is non-empty.
answered Nov 13 '18 at 16:54
Brad SolomonBrad Solomon
13.7k73484
13.7k73484
add a comment |
add a comment |
Using Dataframe constructor flatten you list column, then using isin
pd.DataFrame(dfLists['someList!'].tolist()).isin([1,2]).any(1)
Out[39]:
0 True
1 False
dtype: bool
thx for this answer, found it helpful
– reality stilts
Nov 13 '18 at 17:58
add a comment |
Using Dataframe constructor flatten you list column, then using isin
pd.DataFrame(dfLists['someList!'].tolist()).isin([1,2]).any(1)
Out[39]:
0 True
1 False
dtype: bool
thx for this answer, found it helpful
– reality stilts
Nov 13 '18 at 17:58
add a comment |
Using Dataframe constructor flatten you list column, then using isin
pd.DataFrame(dfLists['someList!'].tolist()).isin([1,2]).any(1)
Out[39]:
0 True
1 False
dtype: bool
Using Dataframe constructor flatten you list column, then using isin
pd.DataFrame(dfLists['someList!'].tolist()).isin([1,2]).any(1)
Out[39]:
0 True
1 False
dtype: bool
answered Nov 13 '18 at 16:55
Wen-BenWen-Ben
1
1
thx for this answer, found it helpful
– reality stilts
Nov 13 '18 at 17:58
add a comment |
thx for this answer, found it helpful
– reality stilts
Nov 13 '18 at 17:58
thx for this answer, found it helpful
– reality stilts
Nov 13 '18 at 17:58
thx for this answer, found it helpful
– reality stilts
Nov 13 '18 at 17:58
add a comment |
You are passing the dataframe a list of lists. It's comparing integers to lists, so it's not finding a match.
Define your columns distinctly, like this, and isin should work.
lists={'someList!':[1,2,12,6,'ABC'], 'someList2':[1000,4,'z','a','bob']}
add a comment |
You are passing the dataframe a list of lists. It's comparing integers to lists, so it's not finding a match.
Define your columns distinctly, like this, and isin should work.
lists={'someList!':[1,2,12,6,'ABC'], 'someList2':[1000,4,'z','a','bob']}
add a comment |
You are passing the dataframe a list of lists. It's comparing integers to lists, so it's not finding a match.
Define your columns distinctly, like this, and isin should work.
lists={'someList!':[1,2,12,6,'ABC'], 'someList2':[1000,4,'z','a','bob']}
You are passing the dataframe a list of lists. It's comparing integers to lists, so it's not finding a match.
Define your columns distinctly, like this, and isin should work.
lists={'someList!':[1,2,12,6,'ABC'], 'someList2':[1000,4,'z','a','bob']}
answered Nov 13 '18 at 16:56
CobraCobra
225
225
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.
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%2f53285716%2fpandas-isin-column-of-lists%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
Your code gives an error instead. Please provide a reproducible code.
– TeeKea
Nov 13 '18 at 16:54