Pandas数据结构 2

我们也可以使用 key/value 对象,类似字典来创建 Series:

实例
import pandas as pd

sites = {1: "Google", 2: "Runoob", 3: "Wiki"}

myvar = pd.Series(sites)

print(myvar)
输出结果如下:

从上图可知,字典的 key 变成了索引值。

如果我们只需要字典中的一部分数据,只需要指定需要数据的索引即可,如下实例:

实例
import pandas as pd

sites = {1: "Google", 2: "Runoob", 3: "Wiki"}

myvar = pd.Series(sites, index = [1, 2])

print(myvar)
输出结果如下:

设置 Series 名称参数:

实例
import pandas as pd

sites = {1: "Google", 2: "Runoob", 3: "Wiki"}

myvar = pd.Series(sites, index = [1, 2], name="RUNOOB-Series-TEST" )

print(myvar)

原文地址:https://www.cnblogs.com/gongyunlong-blogs/p/15695740.html