import pandas as pd
1.series类型(一维数据类型)
(DataFrame是二位数据类型)
series类型由一组数据及与之相关的数据索引组成
1.1 创建series
可以创建类型列表、标量值、元祖、字典、数组ndarray、其他函数。
a=pd.Series([1,2,3,4,5])#列表
b=pd.Series((1,2,3,4,5)) #元祖都可以创建
print(a)
0 1
1 2
2 3
3 4
4 5
#创建标量值
d=pd.Series(10,index=['a','b','c','d'])
print(d)
a 10
b 10
c 10
d 10
dtype: int64
#字典创建
e=pd.Series({'a':1,'b':2,'c':3,'d':4})
print(e)
a 1
b 2
c 3
d 4
dtype: int64
#从数组中创建
import pandas as pd
import numpy as np
f=pd.Series(np.arange(5))
print(f)
2.更改索引号码
c=pd.Series([1,2,3,4,],index=['a','b','c','d'])#index索引参数(用来更改索引标签)
print(c)
a 1
b 2
c 3
d 4
dtype: int64
3.操作
3.1 index
#index操作,获得索引标签
a=ser.index
print(a)
Index(['a', 'b', 'c', 'd'], dtype='object')
3.2 value、索引操作
#value操作,获取数据
b=ser.values
print(b)
[1 2 3 4]
#索引
c=ser['a']#直接索引标签
print(c)
1
#切片
e=ser[:3]
print(e)
a 1
b 2
c 3
dtype: int64
#用标签做索引
f=ser['a':'c']
print(f)
a 1
b 2
c 3
dtype: int64
g=ser.median()#中位数
h=np.exp(ser)#计算指数
#对齐操作
a=pd.Series([2,4,6,8],['a','b','c','d'])
b=pd.Series([2,4,6,8],['a','b','c','d'])
c=a+b
print(c)
a 4
b 8
c 12
d 16
dtype: int64
#为series对象添加名字
e=b.name='b'
#为索引列添加名字
b.index.name='s索引列'
#修改数据
b['c']=12