Pandas中双括号`[[...]]`和单括号`[..]`索引之间的区别

Mik*_*ner 15 python syntax pandas

我对以下代码行的语法感到困惑:

x_values = dataframe[['Brains']]
Run Code Online (Sandbox Code Playgroud)

数据框对象由2列(大脑和身体)组成

Brains Bodies
42     34
32     23
Run Code Online (Sandbox Code Playgroud)

当我打印x_values时,我得到这样的东西:

Brains
0  42
1  32
Run Code Online (Sandbox Code Playgroud)

就dataframe对象的属性和方法而言,我知道pandas文档,但是双括号语法让我感到困惑.

Max*_*axU 16

考虑一下:

来源DF:

In [79]: df
Out[79]:
   Brains  Bodies
0      42      34
1      32      23
Run Code Online (Sandbox Code Playgroud)

选择一列 - 导致Pandas.Series:

In [80]: df['Brains']
Out[80]:
0    42
1    32
Name: Brains, dtype: int64

In [81]: type(df['Brains'])
Out[81]: pandas.core.series.Series
Run Code Online (Sandbox Code Playgroud)

选择DataFrame的子集 - 导致DataFrame:

In [82]: df[['Brains']]
Out[82]:
   Brains
0      42
1      32

In [83]: type(df[['Brains']])
Out[83]: pandas.core.frame.DataFrame
Run Code Online (Sandbox Code Playgroud)

结论:第二种方法允许我们从DataFrame中选择多个列.第一个只是选择单列...

演示:

In [84]: df = pd.DataFrame(np.random.rand(5,6), columns=list('abcdef'))

In [85]: df
Out[85]:
          a         b         c         d         e         f
0  0.065196  0.257422  0.273534  0.831993  0.487693  0.660252
1  0.641677  0.462979  0.207757  0.597599  0.117029  0.429324
2  0.345314  0.053551  0.634602  0.143417  0.946373  0.770590
3  0.860276  0.223166  0.001615  0.212880  0.907163  0.437295
4  0.670969  0.218909  0.382810  0.275696  0.012626  0.347549

In [86]: df[['e','a','c']]
Out[86]:
          e         a         c
0  0.487693  0.065196  0.273534
1  0.117029  0.641677  0.207757
2  0.946373  0.345314  0.634602
3  0.907163  0.860276  0.001615
4  0.012626  0.670969  0.382810
Run Code Online (Sandbox Code Playgroud)

如果我们在列表中只指定一列,我们将获得一个包含一列的DataFrame:

In [87]: df[['e']]
Out[87]:
          e
0  0.487693
1  0.117029
2  0.946373
3  0.907163
4  0.012626
Run Code Online (Sandbox Code Playgroud)

  • 只是为了密封任何可能的混淆,第一种形式相当于`column ='Brains'; df [column]`,第二个相当于`subset = ['Brains']; DF [子集]`.第一个传递一个字符串,第二个传递一个列表.使用`[[`和`]]`并不是一种特殊形式的索引,而是传递的对象是不同的类型. (7认同)

Set*_*ton 7

[[和中,Python没有特殊的语法]]。而是创建一个列表,然后将该列表作为参数传递给DataFrame索引函数。

按照@MaxU的答案,如果将单个字符串传递给DataFrame,则表示该列返回的系列。如果传递字符串列表,则返回包含给定列的DataFrame。

因此,当您执行以下操作时

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

等效于以下内容

# 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])
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,都使用[]操作符对DataFrame进行索引。

Python使用[]运算符进行索引编制和构造列表文字,最终我相信这是您的困惑。外[]df[['Brains']]正在执行的索引,并且内被创建列表。

>>> some_list = ['Brains']
>>> some_list_of_lists = [['Brains']]
>>> ['Brains'] == [['Brains']][0]
True
>>> 'Brains' == [['Brains']][0][0] == [['Brains'][0]][0]
True
Run Code Online (Sandbox Code Playgroud)

我在上面说明的是,Python绝对不会看到[[和解释它。在最后一个令人费解的示例([['Brains'][0]][0])中,没有特殊的][运算符或]][运算符...发生的是

  • 创建一个单元素列表(['Brains']
  • 该列表的第一个元素被索引(['Brains'][0]=> 'Brains'
  • 那被放入另一个列表([['Brains'][0]]=> ['Brains']
  • 然后该列表的第一个元素被索引([['Brains'][0]][0]=> 'Brains'

  • 这应该是公认的答案。我也对语法非常困惑。在理解 double `[[]]` 只是两个 `[]` 之后,这一切都说得通了。例如,`df[ df['col'] == val ]` 返回一个数据帧而不是序列,因为`df['col'] == val` 是一个系列,结果是一系列的系列,即一个数据框。 (2认同)