Two conditional checks inside a Panda data frame access












0














The command below works fine



idx = np.asarray(df.loc[df['lat1'] != '.'].ix[:,0].index)


but I'm trying to do something like this (with 2 conditions):



idx = np.asarray(df.loc[df['lat1'] != '.' and df['state'] == df['state'][0]].ix[:,0].index)


This throws the following traceback:



Traceback (most recent call last):

File "<ipython-input-274-c07cda0be195>", line 1, in <module>
idx = np.asarray(df.loc[df['lat1'] != '.' and df['state'] == df['state'][0]].ix[:,0].index)

File "/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py", line 1573, in __nonzero__
.format(self.__class__.__name__))

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().


I've looked it up but not able to find a suitable tweak for this case. Any leads would be appreciated.



[EDIT]: Based on the suggestion below, when I try this:



df[(df['lat1']!='.') & (df['state']== df['state'][0])]


I get



        lat1    long1          ...          state                county
5 34 11 ... AK Anchorage
7 1 -3 ... AK Anchorage
14 1 -5 ... AK Anchorage
30 7 -3 ... AK Anchorage
44 1 -4 ... AK Anchorage
47 1 -3 ... AK Anchorage
75 1 -4 ... AK Juneau
82 5 -1 ... AK Kenai Peninsula
102 4 -1 ... AK Fairbanks North Star
106 4 -1 ... AK Matanuska Susitna
137 3 -3 ... AK Matanuska Susitna

[11 rows x 5 columns]


How do I extract only the first column containing the indices?










share|improve this question





























    0














    The command below works fine



    idx = np.asarray(df.loc[df['lat1'] != '.'].ix[:,0].index)


    but I'm trying to do something like this (with 2 conditions):



    idx = np.asarray(df.loc[df['lat1'] != '.' and df['state'] == df['state'][0]].ix[:,0].index)


    This throws the following traceback:



    Traceback (most recent call last):

    File "<ipython-input-274-c07cda0be195>", line 1, in <module>
    idx = np.asarray(df.loc[df['lat1'] != '.' and df['state'] == df['state'][0]].ix[:,0].index)

    File "/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py", line 1573, in __nonzero__
    .format(self.__class__.__name__))

    ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().


    I've looked it up but not able to find a suitable tweak for this case. Any leads would be appreciated.



    [EDIT]: Based on the suggestion below, when I try this:



    df[(df['lat1']!='.') & (df['state']== df['state'][0])]


    I get



            lat1    long1          ...          state                county
    5 34 11 ... AK Anchorage
    7 1 -3 ... AK Anchorage
    14 1 -5 ... AK Anchorage
    30 7 -3 ... AK Anchorage
    44 1 -4 ... AK Anchorage
    47 1 -3 ... AK Anchorage
    75 1 -4 ... AK Juneau
    82 5 -1 ... AK Kenai Peninsula
    102 4 -1 ... AK Fairbanks North Star
    106 4 -1 ... AK Matanuska Susitna
    137 3 -3 ... AK Matanuska Susitna

    [11 rows x 5 columns]


    How do I extract only the first column containing the indices?










    share|improve this question



























      0












      0








      0







      The command below works fine



      idx = np.asarray(df.loc[df['lat1'] != '.'].ix[:,0].index)


      but I'm trying to do something like this (with 2 conditions):



      idx = np.asarray(df.loc[df['lat1'] != '.' and df['state'] == df['state'][0]].ix[:,0].index)


      This throws the following traceback:



      Traceback (most recent call last):

      File "<ipython-input-274-c07cda0be195>", line 1, in <module>
      idx = np.asarray(df.loc[df['lat1'] != '.' and df['state'] == df['state'][0]].ix[:,0].index)

      File "/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py", line 1573, in __nonzero__
      .format(self.__class__.__name__))

      ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().


      I've looked it up but not able to find a suitable tweak for this case. Any leads would be appreciated.



      [EDIT]: Based on the suggestion below, when I try this:



      df[(df['lat1']!='.') & (df['state']== df['state'][0])]


      I get



              lat1    long1          ...          state                county
      5 34 11 ... AK Anchorage
      7 1 -3 ... AK Anchorage
      14 1 -5 ... AK Anchorage
      30 7 -3 ... AK Anchorage
      44 1 -4 ... AK Anchorage
      47 1 -3 ... AK Anchorage
      75 1 -4 ... AK Juneau
      82 5 -1 ... AK Kenai Peninsula
      102 4 -1 ... AK Fairbanks North Star
      106 4 -1 ... AK Matanuska Susitna
      137 3 -3 ... AK Matanuska Susitna

      [11 rows x 5 columns]


      How do I extract only the first column containing the indices?










      share|improve this question















      The command below works fine



      idx = np.asarray(df.loc[df['lat1'] != '.'].ix[:,0].index)


      but I'm trying to do something like this (with 2 conditions):



      idx = np.asarray(df.loc[df['lat1'] != '.' and df['state'] == df['state'][0]].ix[:,0].index)


      This throws the following traceback:



      Traceback (most recent call last):

      File "<ipython-input-274-c07cda0be195>", line 1, in <module>
      idx = np.asarray(df.loc[df['lat1'] != '.' and df['state'] == df['state'][0]].ix[:,0].index)

      File "/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py", line 1573, in __nonzero__
      .format(self.__class__.__name__))

      ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().


      I've looked it up but not able to find a suitable tweak for this case. Any leads would be appreciated.



      [EDIT]: Based on the suggestion below, when I try this:



      df[(df['lat1']!='.') & (df['state']== df['state'][0])]


      I get



              lat1    long1          ...          state                county
      5 34 11 ... AK Anchorage
      7 1 -3 ... AK Anchorage
      14 1 -5 ... AK Anchorage
      30 7 -3 ... AK Anchorage
      44 1 -4 ... AK Anchorage
      47 1 -3 ... AK Anchorage
      75 1 -4 ... AK Juneau
      82 5 -1 ... AK Kenai Peninsula
      102 4 -1 ... AK Fairbanks North Star
      106 4 -1 ... AK Matanuska Susitna
      137 3 -3 ... AK Matanuska Susitna

      [11 rows x 5 columns]


      How do I extract only the first column containing the indices?







      python pandas dataframe






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 at 1:33

























      asked Nov 21 at 0:34









      db18

      1467




      1467
























          1 Answer
          1






          active

          oldest

          votes


















          1














          I'm pretty sure this question has already been asked somewhere... But with pandas you can look at two conditions like this.



          df[(df['lat1']!='.') & (df['state']== df['state'][0])]


          You have to do bitwise operations






          share|improve this answer





















          • If you actually run it on a dataset, you'd find it gives an error like this: TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
            – db18
            Nov 21 at 1:11












          • If you post your dataset then it would be easier to understand. But you can probably just fill nas. df = df.fillna('')
            – kradja
            Nov 21 at 1:14












          • Please see EDIT above.
            – db18
            Nov 21 at 1:33










          • I don't understand your question. With the resulting dataframe you only want the first column containing indices? What does that mean? Also if my answer has helped you please upvote.
            – kradja
            Nov 21 at 5:36










          • I was able to solve the problem with your command. Thanks.
            – db18
            Nov 21 at 18:09











          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%2f53403686%2ftwo-conditional-checks-inside-a-panda-data-frame-access%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









          1














          I'm pretty sure this question has already been asked somewhere... But with pandas you can look at two conditions like this.



          df[(df['lat1']!='.') & (df['state']== df['state'][0])]


          You have to do bitwise operations






          share|improve this answer





















          • If you actually run it on a dataset, you'd find it gives an error like this: TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
            – db18
            Nov 21 at 1:11












          • If you post your dataset then it would be easier to understand. But you can probably just fill nas. df = df.fillna('')
            – kradja
            Nov 21 at 1:14












          • Please see EDIT above.
            – db18
            Nov 21 at 1:33










          • I don't understand your question. With the resulting dataframe you only want the first column containing indices? What does that mean? Also if my answer has helped you please upvote.
            – kradja
            Nov 21 at 5:36










          • I was able to solve the problem with your command. Thanks.
            – db18
            Nov 21 at 18:09
















          1














          I'm pretty sure this question has already been asked somewhere... But with pandas you can look at two conditions like this.



          df[(df['lat1']!='.') & (df['state']== df['state'][0])]


          You have to do bitwise operations






          share|improve this answer





















          • If you actually run it on a dataset, you'd find it gives an error like this: TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
            – db18
            Nov 21 at 1:11












          • If you post your dataset then it would be easier to understand. But you can probably just fill nas. df = df.fillna('')
            – kradja
            Nov 21 at 1:14












          • Please see EDIT above.
            – db18
            Nov 21 at 1:33










          • I don't understand your question. With the resulting dataframe you only want the first column containing indices? What does that mean? Also if my answer has helped you please upvote.
            – kradja
            Nov 21 at 5:36










          • I was able to solve the problem with your command. Thanks.
            – db18
            Nov 21 at 18:09














          1












          1








          1






          I'm pretty sure this question has already been asked somewhere... But with pandas you can look at two conditions like this.



          df[(df['lat1']!='.') & (df['state']== df['state'][0])]


          You have to do bitwise operations






          share|improve this answer












          I'm pretty sure this question has already been asked somewhere... But with pandas you can look at two conditions like this.



          df[(df['lat1']!='.') & (df['state']== df['state'][0])]


          You have to do bitwise operations







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 at 0:52









          kradja

          486




          486












          • If you actually run it on a dataset, you'd find it gives an error like this: TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
            – db18
            Nov 21 at 1:11












          • If you post your dataset then it would be easier to understand. But you can probably just fill nas. df = df.fillna('')
            – kradja
            Nov 21 at 1:14












          • Please see EDIT above.
            – db18
            Nov 21 at 1:33










          • I don't understand your question. With the resulting dataframe you only want the first column containing indices? What does that mean? Also if my answer has helped you please upvote.
            – kradja
            Nov 21 at 5:36










          • I was able to solve the problem with your command. Thanks.
            – db18
            Nov 21 at 18:09


















          • If you actually run it on a dataset, you'd find it gives an error like this: TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
            – db18
            Nov 21 at 1:11












          • If you post your dataset then it would be easier to understand. But you can probably just fill nas. df = df.fillna('')
            – kradja
            Nov 21 at 1:14












          • Please see EDIT above.
            – db18
            Nov 21 at 1:33










          • I don't understand your question. With the resulting dataframe you only want the first column containing indices? What does that mean? Also if my answer has helped you please upvote.
            – kradja
            Nov 21 at 5:36










          • I was able to solve the problem with your command. Thanks.
            – db18
            Nov 21 at 18:09
















          If you actually run it on a dataset, you'd find it gives an error like this: TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
          – db18
          Nov 21 at 1:11






          If you actually run it on a dataset, you'd find it gives an error like this: TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
          – db18
          Nov 21 at 1:11














          If you post your dataset then it would be easier to understand. But you can probably just fill nas. df = df.fillna('')
          – kradja
          Nov 21 at 1:14






          If you post your dataset then it would be easier to understand. But you can probably just fill nas. df = df.fillna('')
          – kradja
          Nov 21 at 1:14














          Please see EDIT above.
          – db18
          Nov 21 at 1:33




          Please see EDIT above.
          – db18
          Nov 21 at 1:33












          I don't understand your question. With the resulting dataframe you only want the first column containing indices? What does that mean? Also if my answer has helped you please upvote.
          – kradja
          Nov 21 at 5:36




          I don't understand your question. With the resulting dataframe you only want the first column containing indices? What does that mean? Also if my answer has helped you please upvote.
          – kradja
          Nov 21 at 5:36












          I was able to solve the problem with your command. Thanks.
          – db18
          Nov 21 at 18:09




          I was able to solve the problem with your command. Thanks.
          – db18
          Nov 21 at 18:09


















          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%2f53403686%2ftwo-conditional-checks-inside-a-panda-data-frame-access%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'