Pandas 系列中 tail() 方法有什么作用?
Pandas 系列对象中的 tail 方法用于从系列中检索底部元素。并且此 tail 方法将一个整数作为参数,该参数由变量 n 表示。
根据该 n 值,pandas 系列 tail 方法将返回一个系列对象,其中包含实际系列对象中的 n 个底部元素。
让我们举一个例子,看看这个 tail 方法如何在我们的 pandas 系列对象上工作。
示例
# importing required packages
import pandas as pd
# creating pandas Series object
series = pd.Series(list(range(10,100,4)))
print(series)
print('
Result from tail() method:')
# accessing bottom elements by using til method
print(series.tail())解释
这里我们使用 python 列表和 range 函数创建了一个 pandas 系列对象,并将 tail 方法定义为我们的系列对象,如下所示“series.tail()”。我们空指定 tail 方法,以便我们可以从我们的系列对象中获取 5 个底部元素。让我们检查输出块。
输出
0 10 1 14 2 18 3 22 4 26 5 30 6 34 7 38 8 42 9 46 10 50 11 54 12 58 13 62 14 66 15 70 16 74 17 78 18 82 19 86 20 90 21 94 22 98 dtype: int64 Result from tail() method: 18 82 19 86 20 90 21 94 22 98 dtype: int64
上面的输出块中有两个系列对象,一个是整个系列对象的输出,第二个是 tail 方法的输出。在第二部分,我们可以看到从索引 18 到 22 的 5 个底部元素。
示例
series.tail(2)
解释
我们可以将一个整数值作为参数指定给 tail 方法。它用于限制输出元素的数量。因此,根据此参数,我们可以看到系列对象的特定数量的底部元素。
输出
21 94 22 98 dtype: int64
这些是系列对象的 2 个底部元素,它们是从 tail 方法中检索到的。
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP