Position of element in a list within a Pandas DataFrame












2















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









share|improve this question





























    2















    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









    share|improve this question



























      2












      2








      2








      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









      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 14:32









      Abhi

      2,505320




      2,505320










      asked Nov 25 '18 at 14:24









      BilzRBilzR

      294




      294
























          1 Answer
          1






          active

          oldest

          votes


















          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]]





          share|improve this answer
























          • 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











          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%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









          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]]





          share|improve this answer
























          • 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
















          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]]





          share|improve this answer
























          • 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














          0












          0








          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]]





          share|improve this answer













          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]]






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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 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



















          • 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

















          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




















          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%2f53468449%2fposition-of-element-in-a-list-within-a-pandas-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

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'