pandas DataFrame isin and following row










2















For a given DataFrame, sorted by b and index reset:



df = pd.DataFrame('a': list('abcdef'), 
'b': [0, 2, 7, 3, 9, 15]
).sort_values('b').reset_index(drop=True)
a b
0 a 0
1 b 2
2 d 3
3 c 7
4 e 9
5 f 15


and a list, v



v = list('adf')


I would like to pull out just the rows in v and the following row (if there is one), similar to grep -A1:



 a b
0 a 0
1 b 2
2 d 3
3 c 7
5 f 15


I can do this by concatenating the index from isin and the index from isin plus one, like so:



df[df.index.isin(
np.concatenate(
(df[df['a'].isin(v)].index,
df[df['a'].isin(v)].index + 1)))]


But this is long and not too easy to understand. Is there a better way?










share|improve this question


























    2















    For a given DataFrame, sorted by b and index reset:



    df = pd.DataFrame('a': list('abcdef'), 
    'b': [0, 2, 7, 3, 9, 15]
    ).sort_values('b').reset_index(drop=True)
    a b
    0 a 0
    1 b 2
    2 d 3
    3 c 7
    4 e 9
    5 f 15


    and a list, v



    v = list('adf')


    I would like to pull out just the rows in v and the following row (if there is one), similar to grep -A1:



     a b
    0 a 0
    1 b 2
    2 d 3
    3 c 7
    5 f 15


    I can do this by concatenating the index from isin and the index from isin plus one, like so:



    df[df.index.isin(
    np.concatenate(
    (df[df['a'].isin(v)].index,
    df[df['a'].isin(v)].index + 1)))]


    But this is long and not too easy to understand. Is there a better way?










    share|improve this question
























      2












      2








      2








      For a given DataFrame, sorted by b and index reset:



      df = pd.DataFrame('a': list('abcdef'), 
      'b': [0, 2, 7, 3, 9, 15]
      ).sort_values('b').reset_index(drop=True)
      a b
      0 a 0
      1 b 2
      2 d 3
      3 c 7
      4 e 9
      5 f 15


      and a list, v



      v = list('adf')


      I would like to pull out just the rows in v and the following row (if there is one), similar to grep -A1:



       a b
      0 a 0
      1 b 2
      2 d 3
      3 c 7
      5 f 15


      I can do this by concatenating the index from isin and the index from isin plus one, like so:



      df[df.index.isin(
      np.concatenate(
      (df[df['a'].isin(v)].index,
      df[df['a'].isin(v)].index + 1)))]


      But this is long and not too easy to understand. Is there a better way?










      share|improve this question














      For a given DataFrame, sorted by b and index reset:



      df = pd.DataFrame('a': list('abcdef'), 
      'b': [0, 2, 7, 3, 9, 15]
      ).sort_values('b').reset_index(drop=True)
      a b
      0 a 0
      1 b 2
      2 d 3
      3 c 7
      4 e 9
      5 f 15


      and a list, v



      v = list('adf')


      I would like to pull out just the rows in v and the following row (if there is one), similar to grep -A1:



       a b
      0 a 0
      1 b 2
      2 d 3
      3 c 7
      5 f 15


      I can do this by concatenating the index from isin and the index from isin plus one, like so:



      df[df.index.isin(
      np.concatenate(
      (df[df['a'].isin(v)].index,
      df[df['a'].isin(v)].index + 1)))]


      But this is long and not too easy to understand. Is there a better way?







      python pandas






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 15:31









      AlexAlex

      1,063822




      1,063822






















          1 Answer
          1






          active

          oldest

          votes


















          3














          You can combine the isin condition and the shift (next row) to create the boolean you needed:



          df[df.a.isin(v).pipe(lambda x: x | x.shift())]

          # a b
          #0 a 0
          #1 b 2
          #2 d 3
          #3 c 7
          #5 f 15





          share|improve this answer




















          • 2





            Nice answer: to note, this is functionally equivalent to x = df.a.isin(v); x | x.shift(). pipe is here just for convenience.

            – jpp
            Nov 15 '18 at 15:40












          • So what is x | x.shift() doing here? Is this x OR x.shift()? @jpp

            – Alex
            Nov 15 '18 at 15:47






          • 1





            Yes. x is a Boolean series, | means vectorised "or".

            – jpp
            Nov 15 '18 at 15:48











          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%2f53322764%2fpandas-dataframe-isin-and-following-row%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









          3














          You can combine the isin condition and the shift (next row) to create the boolean you needed:



          df[df.a.isin(v).pipe(lambda x: x | x.shift())]

          # a b
          #0 a 0
          #1 b 2
          #2 d 3
          #3 c 7
          #5 f 15





          share|improve this answer




















          • 2





            Nice answer: to note, this is functionally equivalent to x = df.a.isin(v); x | x.shift(). pipe is here just for convenience.

            – jpp
            Nov 15 '18 at 15:40












          • So what is x | x.shift() doing here? Is this x OR x.shift()? @jpp

            – Alex
            Nov 15 '18 at 15:47






          • 1





            Yes. x is a Boolean series, | means vectorised "or".

            – jpp
            Nov 15 '18 at 15:48
















          3














          You can combine the isin condition and the shift (next row) to create the boolean you needed:



          df[df.a.isin(v).pipe(lambda x: x | x.shift())]

          # a b
          #0 a 0
          #1 b 2
          #2 d 3
          #3 c 7
          #5 f 15





          share|improve this answer




















          • 2





            Nice answer: to note, this is functionally equivalent to x = df.a.isin(v); x | x.shift(). pipe is here just for convenience.

            – jpp
            Nov 15 '18 at 15:40












          • So what is x | x.shift() doing here? Is this x OR x.shift()? @jpp

            – Alex
            Nov 15 '18 at 15:47






          • 1





            Yes. x is a Boolean series, | means vectorised "or".

            – jpp
            Nov 15 '18 at 15:48














          3












          3








          3







          You can combine the isin condition and the shift (next row) to create the boolean you needed:



          df[df.a.isin(v).pipe(lambda x: x | x.shift())]

          # a b
          #0 a 0
          #1 b 2
          #2 d 3
          #3 c 7
          #5 f 15





          share|improve this answer















          You can combine the isin condition and the shift (next row) to create the boolean you needed:



          df[df.a.isin(v).pipe(lambda x: x | x.shift())]

          # a b
          #0 a 0
          #1 b 2
          #2 d 3
          #3 c 7
          #5 f 15






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 15:40

























          answered Nov 15 '18 at 15:36









          PsidomPsidom

          127k1293138




          127k1293138







          • 2





            Nice answer: to note, this is functionally equivalent to x = df.a.isin(v); x | x.shift(). pipe is here just for convenience.

            – jpp
            Nov 15 '18 at 15:40












          • So what is x | x.shift() doing here? Is this x OR x.shift()? @jpp

            – Alex
            Nov 15 '18 at 15:47






          • 1





            Yes. x is a Boolean series, | means vectorised "or".

            – jpp
            Nov 15 '18 at 15:48













          • 2





            Nice answer: to note, this is functionally equivalent to x = df.a.isin(v); x | x.shift(). pipe is here just for convenience.

            – jpp
            Nov 15 '18 at 15:40












          • So what is x | x.shift() doing here? Is this x OR x.shift()? @jpp

            – Alex
            Nov 15 '18 at 15:47






          • 1





            Yes. x is a Boolean series, | means vectorised "or".

            – jpp
            Nov 15 '18 at 15:48








          2




          2





          Nice answer: to note, this is functionally equivalent to x = df.a.isin(v); x | x.shift(). pipe is here just for convenience.

          – jpp
          Nov 15 '18 at 15:40






          Nice answer: to note, this is functionally equivalent to x = df.a.isin(v); x | x.shift(). pipe is here just for convenience.

          – jpp
          Nov 15 '18 at 15:40














          So what is x | x.shift() doing here? Is this x OR x.shift()? @jpp

          – Alex
          Nov 15 '18 at 15:47





          So what is x | x.shift() doing here? Is this x OR x.shift()? @jpp

          – Alex
          Nov 15 '18 at 15:47




          1




          1





          Yes. x is a Boolean series, | means vectorised "or".

          – jpp
          Nov 15 '18 at 15:48






          Yes. x is a Boolean series, | means vectorised "or".

          – jpp
          Nov 15 '18 at 15:48




















          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%2f53322764%2fpandas-dataframe-isin-and-following-row%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







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3

          Museum of Modern and Contemporary Art of Trento and Rovereto