Pandas 中的 Series 含义是什么?
Pandas Series 是一种一维数据结构,它类似于一维 ndarray,能够容纳任何数据类型的同构元素。它可以存储整数、字符串、浮点数、Python 对象等。
Pandas Series 中的每个值都用标签(索引)表示。通过使用这些标签名称,我们可以访问 Pandas Series 中的任何元素。
Pandas Series 的默认索引值是从 0 到 Series 长度减 1,或者我们可以手动设置标签。
示例
import pandas as pd S1 = pd.Series([11,20,32,49,65]) print(S1)
解释
在这个例子中,我们可以看到一个简单的 Python Pandas Series,它使用整数列表。首先,我们使用别名 pd 导入了 Python Pandas 包。pandas.Series() 方法用于创建 Series 对象。
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
输出
0 11 1 20 2 32 3 49 4 65 dtype: int64
在上图中,0,1,2,3,4 是索引值(标签),由 Pandas Series 函数自动创建。数字 11、20、32、49、65 是存储在 Series 对象中的元素,这里所有元素的数据类型都是 int64。
示例
import pandas as pd S = pd.Series({'a':'A','b':'B','c':'C'}) print(S)
使用字符作为元素创建另一个简单的 Python Pandas Series,Python 字典的键会自动作为 Series 索引值。
输出
a A b B c C dtype: object
大写字母和小写字母分别是 Series 元素和标签名称。
Pandas Series 是一种数组类型的对象,它将存储任何数据类型的一维值。在上面的两个例子中,我们看到了整数类型和对象类型的 Series 创建。
广告