import pandas as pd
import numpy as np
Head与Tail
- 默认显示5条数据
- 也可指定显示数据的数量
# 默认显示前5条
series.head()
# 指定显示后3条
series.tail(3)
属性与底层数据
# 将列标签名变为小写
df.columns = [x.lower() for x in df.columns]
.array属性用于提取Index或Series里的数据
s.index.array
s.array
Series
- 是带标签的一维数组
- 轴标签统称为索引
- 调用pd.Series函数即可创建Series:
s = pd.Series(data,index=index)
其中data支持:1、Python字典 2、多维数组 3、标量值(如,5)
当data不同时,index有不同的用法
当data是多维数组时
- index长度必须与data长度一致
- 没有指定index参数时,创建数值型索引,即[0,...,len(data)-1]。
# 指定index参数时
s=pd.Series(np.random.randn(5),index=['a','b','c','d','e'])
s
a 0.710396
b 1.597084
c 0.341957
d 0.467000
e 0.884691
dtype: float64
s.index
Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
# 未指定index参数时
pd.Series(np.random.randn(5))
0 0.159028
1 -0.202146
2 -0.320935
3 0.029248
4 0.135631
dtype: float64
注意:Pandas的索引可以重复
当data是Python字典时
- Series可以用字典实例化
d={
'b':1,'a':0,'c':2}
pd.Series(d)
b 1
a 0
c 2
dtype: int64
- 注意:data为字典,且未设置index参数时,如果Python版本>=3.6且Pandas版本>=0.23,Series按字典的插入顺序排序索引;
如果设置了index参数,则按索引标签提取data里对应的值。
d={
'a':0.,'b':1.,'c':2.}
# 未设置index索引,按字典的插入顺序排序索引
pd.Series(d)
a 0.0
b 1.0
c 2.0
dtype: float64
# 设置了index参数,则按索引标签提取data里对应的值
pd.Series(d,index=['b','c','d','a'])
b 1.0
c 2.0
d NaN
a 0.0
dtype: float64
当data是标量时
- 必须提供索引
- Series按索引长度重复该标量值
pd.Series(5.,index=['a'