创建序列对象
- 转换字典:使用Series()方法将字典转换成为序列对象,字典的key会自动成为series的index。
- 转换列表:则生产的序列对象会自动赋予index值。
转换字典
sdata = {'Ohio': 35000, 'Texas': 71000, 'Oregon':16000, 'Utah': 5000}s0 = pd.Series(sdata)print('利用字典生成的序列对象\n',s0)print("显示该数据结构类型",type(s0))s1 = pd.Series([6, 1, 2, 9])print('利用列表生成的序列对象\n',s1)
利用字典生成的序列对象Ohio 35000Texas 71000Oregon 16000Utah 5000dtype: int64显示该数据结构类型 <class 'pandas.core.series.Series'>利用列表生成的序列对象0 61 12 23 9dtype: int64
转换列表
默认索引。为0,1,2…
s11 = pd.Series([6, 1, 2, 9])s1
0 61 12 23 9dtype: int64
添加字符索引。通过指定index为series增加索引。
s1 = pd.Series([6, 1, 2, 9],index=['a','b','c','d'])s1
a 6b 1c 2d 9dtype: int64
添加数字索引。
s12 = pd.Series([6, 1, 2, 9], index=[10,11,12,13])s12
10 611 112 213 9dtype: int64
s12.drop(10)
11 112 213 9dtype: int64
