一、初始Series
Series 是一个带有 名称 和索引的一维数组,既然是数组,肯定要说到的就是数组中的元素类型,在 Series 中包含的数据类型可以是整数、浮点、字符串、Python对象等。
pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False)
-
创建第一个Series
import pandas as pd user_age = pd.Series(data=[18, 30, 25, 40]) user_age.index = ["Tom", "Bob", "Mary", "James"] #加索引 user_age.index.name = "name" #索引加名字 user_age.name="user_age_info" #series加名字 user_age Out[4]: name Tom 18 Bob 30 Mary 25 James 40 Name: user_age_info, dtype: int64
- 创建Series的方式
- 列表方式创建
pd.Series([],index=[])
- 字典方式创建
pd.Series({}
# 方式一 t = pd.Series([1,2,3,4,43],index=list('asdfg')) print(t) a 1 s 2 d 3 f 4 g 43 dtype: int64 #方式二 temp_dict = {'name':'xiaohong','age':30,'tel':10086} t2 = pd.Series(temp_dict) t2 Out[10]: name xiaohong age 30 tel 10086 dtype: object import string #字典推导式 a = {string.ascii_uppercase[i]:i for i in range(10)} print(a) print(pd.Series(a)) print(pd.Series(a,index=list(string.ascii_uppercase[5:15]))) {'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7, 'I': 8, 'J': 9} A 0 B 1 C 2 D 3 E 4 F 5 G 6 H 7 I 8 J 9 dtype: int64 F 5.0 G 6.0 H 7.0 I 8.0 J 9.0 K NaN L NaN M NaN N NaN O NaN dtype: float64