TypeError: unhashable type: 'slice' pandas DataFrame column










1















This is probably simple but I cant find the explanation and it happens to me all the time.



I am trying to selected values from column Rate1E that are over 3.5 and view the rest of the rows in pandas DataFrame energy for selected rows meeting criteria as stated above. I had someone give me an answer before and have simply changed to text as follows:



energy = energy.loc[energy[:, 'Rate1E'] >= 3.5]
print(energy.loc[:, 'Rate1E'])


However once again I have found myself with the error:



TypeError: unhashable type: 'slice'


Online solutions suggest .loc is the answer. Would someone know how to fix the code or better yet explain to me why I always seem to get that error.



Thanks.










share|improve this question
























  • In general, df[x, y, z] treats x, y, z as a tuple that represents a label when the column index is a MultiIndex. In this case it's trying to use :, i.e., slice(None) as a label.

    – BallpointBen
    Nov 14 '18 at 15:49















1















This is probably simple but I cant find the explanation and it happens to me all the time.



I am trying to selected values from column Rate1E that are over 3.5 and view the rest of the rows in pandas DataFrame energy for selected rows meeting criteria as stated above. I had someone give me an answer before and have simply changed to text as follows:



energy = energy.loc[energy[:, 'Rate1E'] >= 3.5]
print(energy.loc[:, 'Rate1E'])


However once again I have found myself with the error:



TypeError: unhashable type: 'slice'


Online solutions suggest .loc is the answer. Would someone know how to fix the code or better yet explain to me why I always seem to get that error.



Thanks.










share|improve this question
























  • In general, df[x, y, z] treats x, y, z as a tuple that represents a label when the column index is a MultiIndex. In this case it's trying to use :, i.e., slice(None) as a label.

    – BallpointBen
    Nov 14 '18 at 15:49













1












1








1


1






This is probably simple but I cant find the explanation and it happens to me all the time.



I am trying to selected values from column Rate1E that are over 3.5 and view the rest of the rows in pandas DataFrame energy for selected rows meeting criteria as stated above. I had someone give me an answer before and have simply changed to text as follows:



energy = energy.loc[energy[:, 'Rate1E'] >= 3.5]
print(energy.loc[:, 'Rate1E'])


However once again I have found myself with the error:



TypeError: unhashable type: 'slice'


Online solutions suggest .loc is the answer. Would someone know how to fix the code or better yet explain to me why I always seem to get that error.



Thanks.










share|improve this question
















This is probably simple but I cant find the explanation and it happens to me all the time.



I am trying to selected values from column Rate1E that are over 3.5 and view the rest of the rows in pandas DataFrame energy for selected rows meeting criteria as stated above. I had someone give me an answer before and have simply changed to text as follows:



energy = energy.loc[energy[:, 'Rate1E'] >= 3.5]
print(energy.loc[:, 'Rate1E'])


However once again I have found myself with the error:



TypeError: unhashable type: 'slice'


Online solutions suggest .loc is the answer. Would someone know how to fix the code or better yet explain to me why I always seem to get that error.



Thanks.







python pandas dataframe indexing series






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 12 '18 at 13:26







geds133

















asked Nov 14 '18 at 15:36









geds133geds133

818




818












  • In general, df[x, y, z] treats x, y, z as a tuple that represents a label when the column index is a MultiIndex. In this case it's trying to use :, i.e., slice(None) as a label.

    – BallpointBen
    Nov 14 '18 at 15:49

















  • In general, df[x, y, z] treats x, y, z as a tuple that represents a label when the column index is a MultiIndex. In this case it's trying to use :, i.e., slice(None) as a label.

    – BallpointBen
    Nov 14 '18 at 15:49
















In general, df[x, y, z] treats x, y, z as a tuple that represents a label when the column index is a MultiIndex. In this case it's trying to use :, i.e., slice(None) as a label.

– BallpointBen
Nov 14 '18 at 15:49





In general, df[x, y, z] treats x, y, z as a tuple that represents a label when the column index is a MultiIndex. In this case it's trying to use :, i.e., slice(None) as a label.

– BallpointBen
Nov 14 '18 at 15:49












2 Answers
2






active

oldest

votes


















3














Your syntax is incorrect. You need:



energy = energy.loc[energy['Rate1E'] >= 3.5]


Alternatively:



energy = energy.loc[energy.loc[:, 'Rate1E'] >= 3.5]


The crucial point is energy.loc[:, 'Rate1E'] and energy['Rate1E'] are series, the latter being a convenient way to access the former.



It is unfortunate, but the Pandas documentation doesn't specify precisely permitted arguments for pd.DataFrame.__getitem__, for which df is syntactic sugar. The most popular uses are:



  • Provide a string to give a series.

  • Provide a list to give a dataframe.

  • Provide a Boolean series to filter your dataframe.





share|improve this answer
































    0














    Your call of energy[:, 'Rate1E'] is doing something different to what you expect. You're looking for a .loc call.



    energy = energy.loc[energy['Rate1E'] >= 3.5]
    print(energy['Rate1E'])





    share|improve this answer

























    • Solution is incorrect as .loc call needed after initial square brackets.

      – geds133
      Nov 14 '18 at 16:03











    • For solution see above

      – geds133
      Nov 14 '18 at 16:04











    • I am trying to view all other columns in energy that is based on values in unitRate1Elec that are greater or equal to 3.5.

      – geds133
      Nov 14 '18 at 16:16











    • @geds133 Yes, that is what the proposed answer does. @jpp has done a great job of summarizing why you can use both both energy.loc or energy inside your first energy.loc call. Either way it looks like your problem is solved :)

      – cvonsteg
      Nov 14 '18 at 16:18











    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%2f53303755%2ftypeerror-unhashable-type-slice-pandas-dataframe-column%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Your syntax is incorrect. You need:



    energy = energy.loc[energy['Rate1E'] >= 3.5]


    Alternatively:



    energy = energy.loc[energy.loc[:, 'Rate1E'] >= 3.5]


    The crucial point is energy.loc[:, 'Rate1E'] and energy['Rate1E'] are series, the latter being a convenient way to access the former.



    It is unfortunate, but the Pandas documentation doesn't specify precisely permitted arguments for pd.DataFrame.__getitem__, for which df is syntactic sugar. The most popular uses are:



    • Provide a string to give a series.

    • Provide a list to give a dataframe.

    • Provide a Boolean series to filter your dataframe.





    share|improve this answer





























      3














      Your syntax is incorrect. You need:



      energy = energy.loc[energy['Rate1E'] >= 3.5]


      Alternatively:



      energy = energy.loc[energy.loc[:, 'Rate1E'] >= 3.5]


      The crucial point is energy.loc[:, 'Rate1E'] and energy['Rate1E'] are series, the latter being a convenient way to access the former.



      It is unfortunate, but the Pandas documentation doesn't specify precisely permitted arguments for pd.DataFrame.__getitem__, for which df is syntactic sugar. The most popular uses are:



      • Provide a string to give a series.

      • Provide a list to give a dataframe.

      • Provide a Boolean series to filter your dataframe.





      share|improve this answer



























        3












        3








        3







        Your syntax is incorrect. You need:



        energy = energy.loc[energy['Rate1E'] >= 3.5]


        Alternatively:



        energy = energy.loc[energy.loc[:, 'Rate1E'] >= 3.5]


        The crucial point is energy.loc[:, 'Rate1E'] and energy['Rate1E'] are series, the latter being a convenient way to access the former.



        It is unfortunate, but the Pandas documentation doesn't specify precisely permitted arguments for pd.DataFrame.__getitem__, for which df is syntactic sugar. The most popular uses are:



        • Provide a string to give a series.

        • Provide a list to give a dataframe.

        • Provide a Boolean series to filter your dataframe.





        share|improve this answer















        Your syntax is incorrect. You need:



        energy = energy.loc[energy['Rate1E'] >= 3.5]


        Alternatively:



        energy = energy.loc[energy.loc[:, 'Rate1E'] >= 3.5]


        The crucial point is energy.loc[:, 'Rate1E'] and energy['Rate1E'] are series, the latter being a convenient way to access the former.



        It is unfortunate, but the Pandas documentation doesn't specify precisely permitted arguments for pd.DataFrame.__getitem__, for which df is syntactic sugar. The most popular uses are:



        • Provide a string to give a series.

        • Provide a list to give a dataframe.

        • Provide a Boolean series to filter your dataframe.






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 12 '18 at 13:28









        geds133

        818




        818










        answered Nov 14 '18 at 15:44









        jppjpp

        102k2164115




        102k2164115























            0














            Your call of energy[:, 'Rate1E'] is doing something different to what you expect. You're looking for a .loc call.



            energy = energy.loc[energy['Rate1E'] >= 3.5]
            print(energy['Rate1E'])





            share|improve this answer

























            • Solution is incorrect as .loc call needed after initial square brackets.

              – geds133
              Nov 14 '18 at 16:03











            • For solution see above

              – geds133
              Nov 14 '18 at 16:04











            • I am trying to view all other columns in energy that is based on values in unitRate1Elec that are greater or equal to 3.5.

              – geds133
              Nov 14 '18 at 16:16











            • @geds133 Yes, that is what the proposed answer does. @jpp has done a great job of summarizing why you can use both both energy.loc or energy inside your first energy.loc call. Either way it looks like your problem is solved :)

              – cvonsteg
              Nov 14 '18 at 16:18
















            0














            Your call of energy[:, 'Rate1E'] is doing something different to what you expect. You're looking for a .loc call.



            energy = energy.loc[energy['Rate1E'] >= 3.5]
            print(energy['Rate1E'])





            share|improve this answer

























            • Solution is incorrect as .loc call needed after initial square brackets.

              – geds133
              Nov 14 '18 at 16:03











            • For solution see above

              – geds133
              Nov 14 '18 at 16:04











            • I am trying to view all other columns in energy that is based on values in unitRate1Elec that are greater or equal to 3.5.

              – geds133
              Nov 14 '18 at 16:16











            • @geds133 Yes, that is what the proposed answer does. @jpp has done a great job of summarizing why you can use both both energy.loc or energy inside your first energy.loc call. Either way it looks like your problem is solved :)

              – cvonsteg
              Nov 14 '18 at 16:18














            0












            0








            0







            Your call of energy[:, 'Rate1E'] is doing something different to what you expect. You're looking for a .loc call.



            energy = energy.loc[energy['Rate1E'] >= 3.5]
            print(energy['Rate1E'])





            share|improve this answer















            Your call of energy[:, 'Rate1E'] is doing something different to what you expect. You're looking for a .loc call.



            energy = energy.loc[energy['Rate1E'] >= 3.5]
            print(energy['Rate1E'])






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 12 '18 at 14:07









            geds133

            818




            818










            answered Nov 14 '18 at 15:47









            cvonstegcvonsteg

            20616




            20616












            • Solution is incorrect as .loc call needed after initial square brackets.

              – geds133
              Nov 14 '18 at 16:03











            • For solution see above

              – geds133
              Nov 14 '18 at 16:04











            • I am trying to view all other columns in energy that is based on values in unitRate1Elec that are greater or equal to 3.5.

              – geds133
              Nov 14 '18 at 16:16











            • @geds133 Yes, that is what the proposed answer does. @jpp has done a great job of summarizing why you can use both both energy.loc or energy inside your first energy.loc call. Either way it looks like your problem is solved :)

              – cvonsteg
              Nov 14 '18 at 16:18


















            • Solution is incorrect as .loc call needed after initial square brackets.

              – geds133
              Nov 14 '18 at 16:03











            • For solution see above

              – geds133
              Nov 14 '18 at 16:04











            • I am trying to view all other columns in energy that is based on values in unitRate1Elec that are greater or equal to 3.5.

              – geds133
              Nov 14 '18 at 16:16











            • @geds133 Yes, that is what the proposed answer does. @jpp has done a great job of summarizing why you can use both both energy.loc or energy inside your first energy.loc call. Either way it looks like your problem is solved :)

              – cvonsteg
              Nov 14 '18 at 16:18

















            Solution is incorrect as .loc call needed after initial square brackets.

            – geds133
            Nov 14 '18 at 16:03





            Solution is incorrect as .loc call needed after initial square brackets.

            – geds133
            Nov 14 '18 at 16:03













            For solution see above

            – geds133
            Nov 14 '18 at 16:04





            For solution see above

            – geds133
            Nov 14 '18 at 16:04













            I am trying to view all other columns in energy that is based on values in unitRate1Elec that are greater or equal to 3.5.

            – geds133
            Nov 14 '18 at 16:16





            I am trying to view all other columns in energy that is based on values in unitRate1Elec that are greater or equal to 3.5.

            – geds133
            Nov 14 '18 at 16:16













            @geds133 Yes, that is what the proposed answer does. @jpp has done a great job of summarizing why you can use both both energy.loc or energy inside your first energy.loc call. Either way it looks like your problem is solved :)

            – cvonsteg
            Nov 14 '18 at 16:18






            @geds133 Yes, that is what the proposed answer does. @jpp has done a great job of summarizing why you can use both both energy.loc or energy inside your first energy.loc call. Either way it looks like your problem is solved :)

            – cvonsteg
            Nov 14 '18 at 16:18


















            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%2f53303755%2ftypeerror-unhashable-type-slice-pandas-dataframe-column%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

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo