Position of element in a list within a Pandas DataFrame
newbie here
I've got a Pandas DataFrame that contains that contains the column crewjob as below. It looks like each row is a list.
crewjob
[Director, Screenplay, Screenplay, Screenplay,...
[Executive Producer, Screenplay, Original Musi...
[Director, Characters, Writer, Sound Recordist]
[Director, Screenplay, Producer, Producer, Pro...
[Original Music Composer, Director of Photogra...
[Director, Screenplay, Producer, Producer, Ori...
[Director, Screenplay, Producer, Original Musi...
[Screenplay, Screenplay, Director, Novel]
[Director, Screenplay, Screenplay, Producer, P...
I'd like to extract for each row the position of director within the list.
I've tried the enumerate method and it chucks out a blank list.
indices = [i for i, x in enumerate(creditsdf['crewjob']) if x == "Director"]
I've also tried the .index method
creditsdf['test'] = creditsdf['crewjob'].index("Director")
Which gives the following error;
TypeError: 'RangeIndex' object is not callable
python pandas
add a comment |
newbie here
I've got a Pandas DataFrame that contains that contains the column crewjob as below. It looks like each row is a list.
crewjob
[Director, Screenplay, Screenplay, Screenplay,...
[Executive Producer, Screenplay, Original Musi...
[Director, Characters, Writer, Sound Recordist]
[Director, Screenplay, Producer, Producer, Pro...
[Original Music Composer, Director of Photogra...
[Director, Screenplay, Producer, Producer, Ori...
[Director, Screenplay, Producer, Original Musi...
[Screenplay, Screenplay, Director, Novel]
[Director, Screenplay, Screenplay, Producer, P...
I'd like to extract for each row the position of director within the list.
I've tried the enumerate method and it chucks out a blank list.
indices = [i for i, x in enumerate(creditsdf['crewjob']) if x == "Director"]
I've also tried the .index method
creditsdf['test'] = creditsdf['crewjob'].index("Director")
Which gives the following error;
TypeError: 'RangeIndex' object is not callable
python pandas
add a comment |
newbie here
I've got a Pandas DataFrame that contains that contains the column crewjob as below. It looks like each row is a list.
crewjob
[Director, Screenplay, Screenplay, Screenplay,...
[Executive Producer, Screenplay, Original Musi...
[Director, Characters, Writer, Sound Recordist]
[Director, Screenplay, Producer, Producer, Pro...
[Original Music Composer, Director of Photogra...
[Director, Screenplay, Producer, Producer, Ori...
[Director, Screenplay, Producer, Original Musi...
[Screenplay, Screenplay, Director, Novel]
[Director, Screenplay, Screenplay, Producer, P...
I'd like to extract for each row the position of director within the list.
I've tried the enumerate method and it chucks out a blank list.
indices = [i for i, x in enumerate(creditsdf['crewjob']) if x == "Director"]
I've also tried the .index method
creditsdf['test'] = creditsdf['crewjob'].index("Director")
Which gives the following error;
TypeError: 'RangeIndex' object is not callable
python pandas
newbie here
I've got a Pandas DataFrame that contains that contains the column crewjob as below. It looks like each row is a list.
crewjob
[Director, Screenplay, Screenplay, Screenplay,...
[Executive Producer, Screenplay, Original Musi...
[Director, Characters, Writer, Sound Recordist]
[Director, Screenplay, Producer, Producer, Pro...
[Original Music Composer, Director of Photogra...
[Director, Screenplay, Producer, Producer, Ori...
[Director, Screenplay, Producer, Original Musi...
[Screenplay, Screenplay, Director, Novel]
[Director, Screenplay, Screenplay, Producer, P...
I'd like to extract for each row the position of director within the list.
I've tried the enumerate method and it chucks out a blank list.
indices = [i for i, x in enumerate(creditsdf['crewjob']) if x == "Director"]
I've also tried the .index method
creditsdf['test'] = creditsdf['crewjob'].index("Director")
Which gives the following error;
TypeError: 'RangeIndex' object is not callable
python pandas
python pandas
edited Nov 25 '18 at 14:32
Abhi
2,505320
2,505320
asked Nov 25 '18 at 14:24
BilzRBilzR
294
294
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Use nested list comprehension:
indices = [[i for i, x in enumerate(y) if x == "Director"] for y in increditsdf['crewjob']]
print (indices)
[[0], , [0], [0], , [0], [0], [2], [0]]
Thanks jezrael, the above solution works. Out of curiousity is it possible to do this with the .index method?
– BilzR
Nov 25 '18 at 14:41
@BilzR - Yes, but is necessary useindices = [find_element_in_list("Director", y) for y in increditsdf['crewjob']]
from here for avoid error if not exist value in list.
– jezrael
Nov 25 '18 at 14:44
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%2f53468449%2fposition-of-element-in-a-list-within-a-pandas-dataframe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use nested list comprehension:
indices = [[i for i, x in enumerate(y) if x == "Director"] for y in increditsdf['crewjob']]
print (indices)
[[0], , [0], [0], , [0], [0], [2], [0]]
Thanks jezrael, the above solution works. Out of curiousity is it possible to do this with the .index method?
– BilzR
Nov 25 '18 at 14:41
@BilzR - Yes, but is necessary useindices = [find_element_in_list("Director", y) for y in increditsdf['crewjob']]
from here for avoid error if not exist value in list.
– jezrael
Nov 25 '18 at 14:44
add a comment |
Use nested list comprehension:
indices = [[i for i, x in enumerate(y) if x == "Director"] for y in increditsdf['crewjob']]
print (indices)
[[0], , [0], [0], , [0], [0], [2], [0]]
Thanks jezrael, the above solution works. Out of curiousity is it possible to do this with the .index method?
– BilzR
Nov 25 '18 at 14:41
@BilzR - Yes, but is necessary useindices = [find_element_in_list("Director", y) for y in increditsdf['crewjob']]
from here for avoid error if not exist value in list.
– jezrael
Nov 25 '18 at 14:44
add a comment |
Use nested list comprehension:
indices = [[i for i, x in enumerate(y) if x == "Director"] for y in increditsdf['crewjob']]
print (indices)
[[0], , [0], [0], , [0], [0], [2], [0]]
Use nested list comprehension:
indices = [[i for i, x in enumerate(y) if x == "Director"] for y in increditsdf['crewjob']]
print (indices)
[[0], , [0], [0], , [0], [0], [2], [0]]
answered Nov 25 '18 at 14:26
jezraeljezrael
342k25297369
342k25297369
Thanks jezrael, the above solution works. Out of curiousity is it possible to do this with the .index method?
– BilzR
Nov 25 '18 at 14:41
@BilzR - Yes, but is necessary useindices = [find_element_in_list("Director", y) for y in increditsdf['crewjob']]
from here for avoid error if not exist value in list.
– jezrael
Nov 25 '18 at 14:44
add a comment |
Thanks jezrael, the above solution works. Out of curiousity is it possible to do this with the .index method?
– BilzR
Nov 25 '18 at 14:41
@BilzR - Yes, but is necessary useindices = [find_element_in_list("Director", y) for y in increditsdf['crewjob']]
from here for avoid error if not exist value in list.
– jezrael
Nov 25 '18 at 14:44
Thanks jezrael, the above solution works. Out of curiousity is it possible to do this with the .index method?
– BilzR
Nov 25 '18 at 14:41
Thanks jezrael, the above solution works. Out of curiousity is it possible to do this with the .index method?
– BilzR
Nov 25 '18 at 14:41
@BilzR - Yes, but is necessary use
indices = [find_element_in_list("Director", y) for y in increditsdf['crewjob']]
from here for avoid error if not exist value in list.– jezrael
Nov 25 '18 at 14:44
@BilzR - Yes, but is necessary use
indices = [find_element_in_list("Director", y) for y in increditsdf['crewjob']]
from here for avoid error if not exist value in list.– jezrael
Nov 25 '18 at 14:44
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%2f53468449%2fposition-of-element-in-a-list-within-a-pandas-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