The difference between double brace `[[…]]` and single brace `[..]` indexing in Pandas

后端 未结 3 1639
梦如初夏
梦如初夏 2020-12-03 10:53

I\'m confused about the syntax regarding the following line of code:

x_values = dataframe[[\'Brains\']]

The dataframe object consists of 2

3条回答
  •  难免孤独
    2020-12-03 11:02

    There is no special syntax in Python for [[ and ]]. Rather, a list is being created, and then that list is being passed as an argument to the DataFrame indexing function.

    As per @MaxU's answer, if you pass a single string to a DataFrame a series that represents that one column is returned. If you pass a list of strings, then a DataFrame that contains the given columns is returned.

    So, when you do the following

    # Print "Brains" column as Series
    print(df['Brains'])
    # Return a DataFrame with only one column called "Brains"
    print(df[['Brains']])
    

    It is equivalent to the following

    # Print "Brains" column as Series
    column_to_get = 'Brains'
    print(df[column_to_get])
    # Return a DataFrame with only one column called "Brains"
    subset_of_columns_to_get = ['Brains']
    print(df[subset_of_columns_to_get])
    

    In both cases, the DataFrame is being indexed with the [] operator.

    Python uses the [] operator for both indexing and for constructing list literals, and ultimately I believe this is your confusion. The outer [ and ] in df[['Brains']] is performing the indexing, and the inner is creating a list.

    >>> some_list = ['Brains']
    >>> some_list_of_lists = [['Brains']]
    >>> ['Brains'] == [['Brains']][0]
    True
    >>> 'Brains' == [['Brains']][0][0] == [['Brains'][0]][0]
    True
    

    What I am illustrating above is that at no point does Python ever see [[ and interpret it specially. In the last convoluted example ([['Brains'][0]][0]) there is no special ][ operator or ]][ operator... what happens is

    • A single-element list is created (['Brains'])
    • The first element of that list is indexed (['Brains'][0] => 'Brains')
    • That is placed into another list ([['Brains'][0]] => ['Brains'])
    • And then the first element of that list is indexed ([['Brains'][0]][0] => 'Brains')

提交回复
热议问题