Pandas 中双括号`[[...]]` 和单括号`[..]` 索引的区别 [英] The difference between double brace `[[...]]` and single brace `[..]` indexing in Pandas

查看:74
本文介绍了Pandas 中双括号`[[...]]` 和单括号`[..]` 索引的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

x_values = dataframe[['Brains']]

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

The dataframe object consists of 2 columns (Brains and Bodies)

Brains Bodies
42     34
32     23

当我打印 x_values 时,我得到如下信息:

When I print x_values I get something like this:

Brains
0  42
1  32

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

I'm aware of the pandas documentation as far as attributes and methods of the dataframe object are concerned, but the double bracket syntax is confusing me.

推荐答案

考虑一下:

源 DF:

In [79]: df
Out[79]:
   Brains  Bodies
0      42      34
1      32      23

选择一列 - 结果在 Pandas.Series:

Selecting one column - results in 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

选择 DataFrame 的子集 - 结果在 DataFrame:

Selecting subset of DataFrame - results in DataFrame:

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

In [83]: type(df[['Brains']])
Out[83]: pandas.core.frame.DataFrame

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

Conclusion: the second approach allows us to select multiple columns from the DataFrame. The first one just for selecting single column...

演示:

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

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

and if we specify only one column in the list we will get a DataFrame with one column:

In [87]: df[['e']]
Out[87]:
          e
0  0.487693
1  0.117029
2  0.946373
3  0.907163
4  0.012626

这篇关于Pandas 中双括号`[[...]]` 和单括号`[..]` 索引的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆