What the difference between df[:,’column_name’] and df[‘column_name’] when querying data from a pandas.dataframe

up vote
0
down vote

favorite

What the difference between df[:,'column_name'] and df['column_name'] when querying data from pandas’s DataFrame.

When I use df[:,'Energy_supply'] I come into this

TypeError: unhashable type: ‘slice’.

But when I use df['column_name'] I get what I want.

My data is a 228 rows × 3 columns dataframe with a country_name index and the three columns’ name are ['Energy Supply', 'Energy Supply per Capita', '% Renewable'].enter image description here

share|improve this question

  • df[:,’index’] is for accessing elements from an array
    – Nikhil Verma
    Jun 8 at 12:25

  • Welcome on SO, please add the image directly in the question. Also take the tour and see How to Ask.
    – Y0da
    Jun 8 at 12:31

up vote
0
down vote

favorite

What the difference between df[:,'column_name'] and df['column_name'] when querying data from pandas’s DataFrame.

When I use df[:,'Energy_supply'] I come into this

TypeError: unhashable type: ‘slice’.

But when I use df['column_name'] I get what I want.

My data is a 228 rows × 3 columns dataframe with a country_name index and the three columns’ name are ['Energy Supply', 'Energy Supply per Capita', '% Renewable'].enter image description here

share|improve this question

  • df[:,’index’] is for accessing elements from an array
    – Nikhil Verma
    Jun 8 at 12:25

  • Welcome on SO, please add the image directly in the question. Also take the tour and see How to Ask.
    – Y0da
    Jun 8 at 12:31

up vote
0
down vote

favorite

up vote
0
down vote

favorite

What the difference between df[:,'column_name'] and df['column_name'] when querying data from pandas’s DataFrame.

When I use df[:,'Energy_supply'] I come into this

TypeError: unhashable type: ‘slice’.

But when I use df['column_name'] I get what I want.

My data is a 228 rows × 3 columns dataframe with a country_name index and the three columns’ name are ['Energy Supply', 'Energy Supply per Capita', '% Renewable'].enter image description here

share|improve this question

What the difference between df[:,'column_name'] and df['column_name'] when querying data from pandas’s DataFrame.

When I use df[:,'Energy_supply'] I come into this

TypeError: unhashable type: ‘slice’.

But when I use df['column_name'] I get what I want.

My data is a 228 rows × 3 columns dataframe with a country_name index and the three columns’ name are ['Energy Supply', 'Energy Supply per Capita', '% Renewable'].enter image description here

python pandas dataframe

share|improve this question

share|improve this question

share|improve this question

share|improve this question

edited Jun 8 at 12:54

U9-Forward

11.4k2834

11.4k2834

asked Jun 8 at 12:18

LIU ZHIWEN

32

32

  • df[:,’index’] is for accessing elements from an array
    – Nikhil Verma
    Jun 8 at 12:25

  • Welcome on SO, please add the image directly in the question. Also take the tour and see How to Ask.
    – Y0da
    Jun 8 at 12:31

  • df[:,’index’] is for accessing elements from an array
    – Nikhil Verma
    Jun 8 at 12:25

  • Welcome on SO, please add the image directly in the question. Also take the tour and see How to Ask.
    – Y0da
    Jun 8 at 12:31

df[:,’index’] is for accessing elements from an array
– Nikhil Verma
Jun 8 at 12:25

df[:,’index’] is for accessing elements from an array
– Nikhil Verma
Jun 8 at 12:25

Welcome on SO, please add the image directly in the question. Also take the tour and see How to Ask.
– Y0da
Jun 8 at 12:31

Welcome on SO, please add the image directly in the question. Also take the tour and see How to Ask.
– Y0da
Jun 8 at 12:31

2 Answers
2

active

oldest

votes

up vote
0
down vote

accepted

df['index'] 

is for accessing to the column ‘index’ not for specific data.

Note that in python (Numpy), you can access the data by df[:, 2] [line, column]

In Pandas use, instead :

df.loc[:, 'index'] # [line number, column 'index'], : means all

share|improve this answer

    up vote
    0
    down vote

    df[:,'index']
    

    is for accessing elements from an array (slice operation), index is the column index
    (0 to n-1)

    Equivalent operation for pandas is

    df.loc[:,int_index]
    
    
    df['column_name'] is for accessing columns from a pandas data frame.
    

    Have a look at this:

    Using iloc, loc, & ix to select rows and columns in Pandas DataFrames

    share|improve this answer

      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’,
      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%2f50760511%2fwhat-the-difference-between-df-column-name-and-dfcolumn-name-when-query%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

      up vote
      0
      down vote

      accepted

      df['index'] 
      

      is for accessing to the column ‘index’ not for specific data.

      Note that in python (Numpy), you can access the data by df[:, 2] [line, column]

      In Pandas use, instead :

      df.loc[:, 'index'] # [line number, column 'index'], : means all
      

      share|improve this answer

        up vote
        0
        down vote

        accepted

        df['index'] 
        

        is for accessing to the column ‘index’ not for specific data.

        Note that in python (Numpy), you can access the data by df[:, 2] [line, column]

        In Pandas use, instead :

        df.loc[:, 'index'] # [line number, column 'index'], : means all
        

        share|improve this answer

          up vote
          0
          down vote

          accepted

          up vote
          0
          down vote

          accepted

          df['index'] 
          

          is for accessing to the column ‘index’ not for specific data.

          Note that in python (Numpy), you can access the data by df[:, 2] [line, column]

          In Pandas use, instead :

          df.loc[:, 'index'] # [line number, column 'index'], : means all
          

          share|improve this answer

          df['index'] 
          

          is for accessing to the column ‘index’ not for specific data.

          Note that in python (Numpy), you can access the data by df[:, 2] [line, column]

          In Pandas use, instead :

          df.loc[:, 'index'] # [line number, column 'index'], : means all
          

          share|improve this answer

          share|improve this answer

          share|improve this answer

          edited Nov 9 at 19:40

          marc_s

          569k12811001250

          569k12811001250

          answered Jun 8 at 12:38

          Wariored

          333112

          333112

              up vote
              0
              down vote

              df[:,'index']
              

              is for accessing elements from an array (slice operation), index is the column index
              (0 to n-1)

              Equivalent operation for pandas is

              df.loc[:,int_index]
              
              
              df['column_name'] is for accessing columns from a pandas data frame.
              

              Have a look at this:

              Using iloc, loc, & ix to select rows and columns in Pandas DataFrames

              share|improve this answer

                up vote
                0
                down vote

                df[:,'index']
                

                is for accessing elements from an array (slice operation), index is the column index
                (0 to n-1)

                Equivalent operation for pandas is

                df.loc[:,int_index]
                
                
                df['column_name'] is for accessing columns from a pandas data frame.
                

                Have a look at this:

                Using iloc, loc, & ix to select rows and columns in Pandas DataFrames

                share|improve this answer

                  up vote
                  0
                  down vote

                  up vote
                  0
                  down vote

                  df[:,'index']
                  

                  is for accessing elements from an array (slice operation), index is the column index
                  (0 to n-1)

                  Equivalent operation for pandas is

                  df.loc[:,int_index]
                  
                  
                  df['column_name'] is for accessing columns from a pandas data frame.
                  

                  Have a look at this:

                  Using iloc, loc, & ix to select rows and columns in Pandas DataFrames

                  share|improve this answer

                  df[:,'index']
                  

                  is for accessing elements from an array (slice operation), index is the column index
                  (0 to n-1)

                  Equivalent operation for pandas is

                  df.loc[:,int_index]
                  
                  
                  df['column_name'] is for accessing columns from a pandas data frame.
                  

                  Have a look at this:

                  Using iloc, loc, & ix to select rows and columns in Pandas DataFrames

                  share|improve this answer

                  share|improve this answer

                  share|improve this answer

                  answered Jun 8 at 12:27

                  Nikhil Verma

                  1,2081027

                  1,2081027

                      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%2f50760511%2fwhat-the-difference-between-df-column-name-and-dfcolumn-name-when-query%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

                      Related Post

                      Leave a Reply

                      Your email address will not be published. Required fields are marked *