Pandas的两大主要数据结构 Series和DateFrame,其中Series是带标签的一维数组,可存储整数、浮点数、字符串、Python 对象等类型的数据。
Pandas Series 类似表格中的一个列(column),类似于一维数组,可以保存任何数据类型。
Series 由索引(index)和列组成,函数如下:
pandas.Series( data, index, dtype, name, copy)参数说明:
data:一组数据(ndarray 类型)。
index:数据索引标签,如果不指定,默认从 0 开始。
dtype:数据类型,默认会自己判断。
name:设置名称。
copy:拷贝数据,默认为 False。
创建数组
from pandas import Series
a = [2,3,4,5]
s = Series(a)
0 2
1 3
2 4
3 5
dtype: int64
列出数组索引index信息
s.index
RangeIndex(start=0, stop=4, step=1)
列出数组values值
s.values
array([2, 3, 4, 5], dtype=int64)
索引(index)和值(values)组成Series
s1 = Series(a, index=['A','B','C','D'])
A 2
B 3
C 4
D 5
dtype: int64
列出Series-s1索引
s1.index
Index(['A', 'B', 'C', 'D'], dtype='object')
定义字典并Series格式化s3
d = {'A':3,'B':3,'C':2,'D':1}
s3 = Series(d)
A 3
B 3
C 2
D 1
dtype: int64
列出Series-s3索引
s3.index
Index(['A', 'B', 'C', 'D'], dtype='object')
列出Series-s3值
s3.values
array([3, 3, 2, 1], dtype=int64)
Series-s3字典格式化
s3.to_dict()
{'A': 3, 'B': 3, 'C': 2, 'D': 1}
查询字典中索引为A的值
s3['A']
3
列出s3中大于1的值
s3[s3>1]
A 3
B 3
C 2
dtype: int64
-----------------------------------
type(s3[s3>1])
pandas.core.series.Series
s3最大值和最小值
s3.max()
3
-------------
s3.min()
1
s3移除某个索引
s3.pop('C')
2
-------------------------
print(s3)
A 3
B 3
D 1
dtype: int64
创建索引(index)指定和值(values)组成Series s4
s4 = Series(s3, index=['E','F','H'])
E NaN
F NaN
H NaN
dtype: float64
创建索引(index)指定和值(values)组成Series s5
s5 = Series(s3, index=['A','B','C'])
A 3.0
B 3.0
C NaN
dtype: float64
查询s4\s5是否为空
s5.isnull()
A False
B False
C True
dtype: bool
--------------------------
s5.notnull()
A True
B True
C False
dtype: bool
import numpy as np
s2 = Series(np.arange(5))
0 0
1 1
2 2
3 3
4 4
dtype: int32
s2.values
array([0, 1, 2, 3, 4])