参考链接: 用Pandas建立索引并选择数据

In Numpy arrays, we are familiar with the concepts of indexing, slicing, and masking, etc. Similarly, Pandas to supports indexing in their Dataframe. If we are familiar with the indexing in Numpy arrays, the indexing in Pandas will be very easy.

在Numpy数组中,我们熟悉索引,切片和遮罩等概念。类似地,Pandas支持在其Dataframe中建立索引。 如果我们熟悉Numpy数组中的索引编制,那么Pandas中的索引编制将非常容易。

What is Indexing in Python?

什么是Python中的索引编制?

Selecting values from particular rows and columns in a dataframe is known as Indexing. By using Indexing, we can select all rows and some columns or some rows and all columns.

从数据框中的特定行和列中选择值称为索引。 通过使用索引,我们可以选择所有行和某些列,或者选择某些行和所有列。

Let’s create a sample data in a series form for better understanding of indexing.

让我们以系列形式创建样本数据,以更好地理解索引。

The output series looks like this,

输出系列看起来像这样,

1    a3    b5    cdtype: object

Now, here Python offers two types of indices

现在,这里Python提供了两种类型的索引

Explicit Explicit  Implicit Implicit

Explicit Indexing:

明确索引:

For the above dataset if we pass the command as,

对于上述数据集,如果我们将命令传递为

ds[1] it uses explicit indices

ds[1]它使用显式索引

# If we pass the above command ds[1], the output will be'a'

This is Explicit Indexing. Whereas, if we pass the command ds[1:3] it will use the implicit index style,

这是显式索引。 而如果我们传递命令ds[1:3] ,它将使用隐式索引样式,

The output for the command ds[1:3] will be,

命令ds[1:3]的输出为

3    b5    cdtype: object

These slicing and indexing can lead to some sort of confusion. To avoid this, Python offers some special indexer attributes:

这些切片和索引编制可能导致某种混乱。 为了避免这种情况,Python提供了一些特殊的indexer属性:

loc 位置

The loc attribute allows indexing and slicing that always references the explicit index

loc属性允许始终引用显式索引的索引和切片

iloc iloc

The iloc attribute allows indexing and slicing that always references the implicit index style

iloc属性允许始终引用隐式索引样式的索引和切片

One common expression in Python code that everyone follows and practices is “explicit is better than implicit.”

每个人都遵循并实践的Python代码中的一个常见表达是“明确胜于隐含”。

Let’s take a sample dataset and see how indexing can be performed in different formats.

让我们以一个样本数据集为例,看看如何以不同的格式执行索引。

We are using the data of NBA players from kaggle.

我们正在使用kaggle提供的NBA球员数据。

The Dataset looks like this,

数据集看起来像这样,

NBA Players sample dataset

NBA球员样本数据集

单列 (Single Column)

To display a single column from the dataframe, we will mention the column name in the print statement.

为了显示数据框中的单个列,我们将在print语句中提及列名。

The output will look like this,

输出将如下所示:

Single Column from Dataset

数据集中的单列

多列 (Multiple Columns)

Let’s try to display the ‘Age’, ‘College’ and ‘Draft Year’ of the players. We can display multiple columns in the following way,

让我们尝试显示球员的“年龄”,“大学”和“起草年”。 我们可以通过以下方式显示多列,

The multiple columns will display like this,

多列将显示如下,

Multiple Columns

多列

.loc方法 (.loc Method)

Indexing using .loc method. If we use the .loc method, we have to pass the data using its Label name.

使用.loc方法建立索引。 如果使用.loc方法,则必须使用其Label名称传递数据。

单排 (Single Row)

To display a single row from the dataframe, we will mention the row’s index name in the .loc method.

为了显示数据框中的一行,我们将在.loc方法中提及该行的索引名称。

The whole row information will display like this,

整行信息将像这样显示,

Single Row information

单行信息

多行 (Multiple Rows)

Same as single row, pass the rows information in the print command to display the information.

与单行相同,在print命令中传递行信息以显示信息。

The output will be,

输出将是

Multiple Rows

多行

选择行和列 (Selecting Rows and Columns)

We can also select multiple rows and multiple columns at a time using the .loc method.

我们还可以使用.loc方法一次选择多个行和多个列。

The output will be like this,

输出将是这样,

Displaying Rows and Columns

显示行和列

所有行和某些列 (All Rows and Some Columns)

To display all the rows with some columns using the .loc method.

使用.loc方法显示带有某些列的所有行。

The output of the above code will be like this,

上面代码的输出将是这样,

The same output can be achieved by simply giving column names without using the .loc method as shown in Selecting Multiple Columns.

只需提供列名而无需使用 选择多列中所示的 .loc 方法 ,就可以实现相同的输出。

The output will be same as the one above,

输出将与上面的输出相同,

Same output without .loc

没有.loc的相同输出

.iloc方法 (.iloc Method)

Indexing using the .iloc method. If we use the .iloc method, we have to pass the data using its Position. It is very similar to the .loc method, the only difference is .iloc uses integers to extract the information.

使用.iloc方法建立索引。 如果使用.iloc方法,则必须使用其Position传递数据。 它与.loc方法非常相似,唯一的区别是.iloc使用整数来提取信息。

单排 (Single Row)

We have to pass a single integer in the .iloc method to get the row information.

我们必须在.iloc方法中传递一个整数以获取行信息。

The output will be,

输出将是

Single Row using .iloc method

单行使用.iloc方法

多行 (Multiple Rows)

To select multiple rows, we have to pass the positions of the selected rows.

要选择多行,我们必须传递所选行的位置。

The output will look something like this,

输出看起来像这样,

选择行和列 (Selecting Rows and Columns)

To display a specific number of rows and columns, we create a list of integer for rows and a list of integer for columns and pass to iloc function.

要显示特定数量的行和列,我们为行创建一个整数列表,为列创建一个整数列表,然后传递给iloc函数。

The output will be,

输出将是

Rows and Columns iloc method

行和列iloc方法

所有行和某些列 (All Rows and Some Columns)

To display all the rows, we have to pass “:” and the integers for columns.

要显示所有行,我们必须传递“:”和列的整数。

The output will look like something like this,

输出看起来像这样,

All Rows and Some columns iloc

所有行和某些列iloc

If the columns in the dataset are of “int” or “float” type, then we can apply all the numeric operations to the column directly and manipulate the data to our requirements.

如果数据集中的列为“ int”或“ float”类型,则我们可以将所有数值运算直接应用于该列,并根据需要操作数据。

使用.loc方法的数值运算 (Numeric Operations using .loc method)

The output will be like this,

输出将是这样,

Numeric operations using loc function

使用loc函数进行数值运算

使用.iloc方法的数值运算 (Numeric operations using .iloc Method)

The output will be same as the previous one with .loc method

输出将与使用.loc方法的上一个输出相同

结论 (Conclusion)

We can conclude this article in three simple statements.

我们可以用三个简单的语句来结束本文。

To avoid confusion on Explicit Indices and Implicit Indices we use .loc and .iloc methods. 为避免对显式索引和隐式索引造成混淆,我们使用.loc和.iloc方法。 .loc method is used for label based indexing. .loc方法用于基于标签的索引。 .iloc method is used for position based indexing. .iloc方法用于基于位置的索引。

These are the three main statements, we need to be aware of while using indexing methods for a Pandas Dataframe in Python.

这是三个主要语句,我们在使用Python中的Pandas Dataframe的索引方法时需要注意。

Thank you for reading and Happy Coding!!!

感谢您的阅读和快乐编码!!!

在这里查看我以前关于Python的文章 (Check out my previous articles about Python here)

Seaborn: Python Seaborn:Python Pandas: Python 熊猫:Python Matplotlib: Python Matplotlib:Python NumPy: Python NumPy:Python Data Visualization and its Importance: Python 数据可视化及其重要性:Python Time Complexity and Its Importance in Python 时间复杂度及其在Python中的重要性 Python Recursion or Recursive Function in Python Python中的Python递归或递归函数

翻译自: https://towardsdatascience.com/indexing-in-pandas-dataframe-using-python-63dcc6242323

[转载] 使用Python在Pandas Dataframe中建立索引相关推荐

  1. python dataframe增加一行_python - 在pandas.DataFrame中添加一行

    python - 在pandas.DataFrame中添加一行 据我所知,pandas旨在加载完全填充的DataFrame,但我需要创建一个空的DataFrame,然后逐个添加行.做这个的最好方式是什 ...

  2. python交换两列的位置_如何更改 pandas dataframe 中两列的位置

    如何更改 pandas dataframe 中两列的位置: 把其中的某列移到第一列的位置. 原来的 df 是: df = pd.read_csv('I:/Papers/consumer/codeand ...

  3. 在Pandas DataFrame中重塑数据

    目录 介绍 透视Pandas DataFrame 在Pandas DataFrame中对数据进行分组 总结 使用我们的数据集后,我们将快速查看可以使用流行的Python库从数据集轻松创建的可视化,然后 ...

  4. 清理Pandas DataFrame中的数据

    目录 介绍 清理按键 查找不一致的数据 总结 确保整个DataFrame一致是很重要的.这包括确保数据的类型正确,消除不一致之处并标准化值. 下载CSV和数据库文件-127.8 KB 下载源代码122 ...

  5. pandas dataframe 中 explode()函数用法及效果

            最近在使用pyspark处理数据,需要连接各种各样的表和字段,因此记录相关函数的使用情况.今天介绍explode(). 1. explode()函数简介 explode 函数是 pan ...

  6. pandas使用drop函数删除dataframe中指定索引列表对应位置的数据行(drop multiple rows in dataframe with integer index)

    pandas使用drop函数删除dataframe中指定索引列表对应位置的数据行(drop multiple rows in dataframe with  integer index) 目录

  7. python将pandas dataframe内容写入ElasticSearch实战

    python将pandas dataframe内容写入ElasticSearch实战 目录 python将pandas dataframe内容写入ElasticSearch实战 索引设置 数据写入 数 ...

  8. pandas dataframe中的列进行重新排序、倒排、正排、自定义排序详解及实践

    pandas dataframe中的列进行重新排序,pandas dataframe列重排.倒排.正排.自定义排序详解及实践 实施数据构建: import pandas as pd import nu ...

  9. 更改 pandas dataframe 中两列的位置

    更改 pandas dataframe 中两列的位置: 把其中的某列移到第一列的位置. 原来的 df 是: df = pd.read_csv('I:/Papers/consumer/codeandpa ...

最新文章

  1. combox qt 引起的删除失败_关于QT的QCombox的掉坑出坑
  2. 在 Ubuntu 14.04/15.04 上配置 Node JS v4.0.0
  3. Python即Python解释器的发展史
  4. 【项目管理】敏捷原则
  5. 教育孩子的一个很棒的方式
  6. 回文字符串啊~---太搞了 少年 DXH
  7. CodeForces - 1484E Skyline Photo(dp+单调栈)
  8. Android 核心已经从 Linux kernel 代码库中删除
  9. 软件工程 敏捷的酒后问答
  10. 前端中对象的几种创建方式
  11. 无法安装 计算机缺失,还原安装程序Windows缺失的文件 - Windows Client | Microsoft Docs...
  12. 一步一步学Ruby(五): Class, Module, Object,Kernel的关系
  13. SSM+Jedis初体验
  14. 云表中表单配置内嵌浏览器
  15. Educational Codeforces Round 77 D.A Game with Traps(二分+差分+前缀和)
  16. 最强半自动化抓鸡工具打造思路
  17. 内网环境中公网域名解析成内网地址的问题
  18. connect的中文意思是什么_connect是什么意思
  19. Windows 连接了网络浏览器不能上网
  20. OpenGL纹理详解

热门文章

  1. 【codevs1048】石子归并
  2. 电大计算机专业毕业自我鉴定,电大毕业生计算机专业自我鉴定
  3. using namespace std;的陷阱
  4. 分治法 ——查找问题 —— 寻找两个等长有序序列的中位数
  5. 抽象类及继承(本科生和研究生类)
  6. Project Euler 628: Open chess positions(公式)
  7. 2018年全国多校算法寒假训练营练习比赛(第一场)F. 大吉大利,今晚吃鸡——跑毒篇(模拟)
  8. 2017CCPC哈尔滨 M:Geometry Problem(随机)
  9. bzoj 1627: [Usaco2007 Dec]穿越泥地(BFS)
  10. java实现socket连接,向指定主机指定端口发送socket数据,并获取响应数据